UIWebView does not load the URL - iphone

I have very small problem while loading the URL in UIWebView.
I have one UIButton, on clicking of it, I add the UIView which contains UIWebView, UIButton & Title bar. Code used is as follows -
[self.view addSubview:vwOnline];
vwOnline.bounds = self.view.bounds;
//Load webview
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#", objWine.strOnlineURL]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[wbvwOnline loadRequest:request];
- (void)webViewDidStartLoad:(UIWebView *)webView
{
//Show activity indicator
[indicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//Remove Activity indicator
[indicator stopAnimating];
[indicator removeFromSuperview];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"Error - %#", error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Find A Vino" message:#"Error while loading request." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
alert.tag = 1;
//Remove Activity indicator
[indicator stopAnimating];
[indicator removeFromSuperview];
}
By doing above code, most of times the UIWebView does not loads the URL which I passed in the object objWine.strOnlineURL. If I clicked the back button & again clicked on the button to load the URL, it goes into the - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error delegate method of UIWebView.
If anyone knows the solution, then please help me.

Instead of adding your webview on your button click each time. Why don't you add your webview into your viewDidLoad and hide and show it in your button click.

when you are going back, make request to nil and load empty htmlstring to webview. One more thing, remove [indicator removeFromSuperview]; line from
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
-(void)webViewDidFinishLoad:(UIWebView *)webView

use
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.google.com"]];
if it is work fine then check objWine.strOnlineURL
otherwise it is clear :)

Related

How to show loading image while loading the content of the web view in iPhone application

I have an url which need to access in web view and the url is accessing successfully. but the issue is time is taking more to load the url in webview and hence I need to show a loading image while loading an url. I am trying for that, but not able to get it to show loading bar, loading icon or whatever it may be.
And my code is here goes
NSLog(#"Response ==> %#" ,encodedString);
//'encodedstring' is my url which need to access
// code to show a loading image
indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.frame = CGRectMake(0, 0, 320, 470);
[self.view addSubview:indicator];
[indicator release];
[indicator startAnimating];
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, 320, 470)];
[webView setDelegate:self];
NSString* urlTwo = [[encodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
stringByReplacingOccurrencesOfString:#"%22" withString:#""];
NSURL *url2;
url2 = [[NSURL alloc] initWithString:urlTwo];
NSLog(#"url2:%#", url2);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url2];
[webView loadRequest:requestObj];
[[self view] addSubview:webView];
}
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[indicator stopAnimating];
[[self view] addSubview:webView];
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
[indicator startAnimating];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[webView stopLoading];
}
Can anybody is here to solve my issue and make the day happy. Thanks in advance.
-(void)webViewDidFinishLoad:(UIWebView *)aWebView
{
[indicator stopAnimating];
}
-(void)webViewDidStartLoad:(UIWebView *)aWebView
{
[indicator startAnimating];
}
-(void)webView:(UIWebView *)aWebView didFailLoadWithError:(NSError *)error
{
[activityView stopAnimating];
}
have u tried that?

how to change UIWebView Tap events

How Can I change the UIWebView tap action Controls.like when I tap and hold on some link in UIWebView ..it opens a UIActionSheet with three options open copy add to reading list ...I need to change this UIActionsheet controls like ..I need to add one more button into this ...how to do that...how to disable this and add new UIActionSheet according to my choice...
code
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *requestURL = [ request URL];
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
// Call your custom actionsheet and use the requestURL to do what you want :)
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#"Contextual Menu"
delegate:self cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil otherButtonTitles:nil];
[sheet addButtonWithTitle:#"Save Page as Bookmark"];
[sheet addButtonWithTitle:#"Open Page in Safari"];
[sheet showInView:webView];
return NO;
}
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com"]]];
webview.delegate=self;
}
- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == actionSheet.cancelButtonIndex) { return; }
switch (buttonIndex) {
case 0:
{
NSLog(#"Item A Selected");
NSLog(#"reg%#", request);
NSURL *requestURL = [request URL];
[webview loadRequest:[NSURLRequest requestWithURL:requestURL]];
break;
}
case 1:
{
NSLog(#"Item B Selected");
break;
}
}
}
You can do like this , catch a tap on link and use your actionsheet then
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *requestURL = [ request URL];
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
// Call your custom actionsheet and use the requestURL to do what you want :)
return NO;
}
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
webview.delegate=self;
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com"]]];
}

UIWebView only calls didFailLoadWithError once

I have a native iPhone app that just loads a website using UIWebView. I implemented the UIWebViewDelegate function didFailLoadWithError to display an error message when the user attempts to use the app without an Internet connection. The alert box shows up the first time I build and run the app, but does not appear thereafter. If I click the home button, then tap my app again, I just see a blank UIWebView. It's as if it's not trying to load the page again, so it never triggers didFailLoadWithError. This is just a simple app without saving and reloading of state, so shouldn't reloading the app re-run all my code, including the attempted loading of the website?
- (void)viewDidLoad {
NSString* urlString = [NSString stringWithString:#"http://www.site.com"];
NSURL* url = [NSURL URLWithString:urlString];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
[super viewDidLoad];
}
#pragma mark UIWebView delegate
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
//[myActivityIndicatorView stopAnimating];
NSLog([NSString stringWithFormat: #"%d", [error code]]);
if ([error code] != USER_STOPPED_LOAD) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Connection Error!"
message:#"You must be connected to the Internet to use Site."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
No because it only gets sent to the background when you click the Home button! To restart the app, kill it using the multitasking bar.

XCode webview connection/server error handling

All,
I am fairly new to XCode and am trying to get a handle on how to best deal with connection issues when trying to use a WebView. I know there are related questions on SO, but none seem to offer complete solutions. I have the following code, but it seems a little inefficient. Hopefully, someone can help me refactor it down to a point where it can be usable anywhere that a UIWebView is called.
NOTE: Please ignore memory issues for now. I realize that has to be added as well.
- (void)viewDidLoad {
[webView setDelegate:self];
NSString *urlAddress = #"http://www.somesite.com/somepage";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
[super viewDidLoad];
}
// Check for URLConnection failure
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *connectionError = [[UIAlertView alloc] initWithTitle:#"Connection error" message:#"Error connecting to page. Please check your 3G and/or Wifi settings." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[connectionError show];
webView.hidden = true;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
//Check for server error
if ([httpResponse statusCode] >= 400) {
UIAlertView *serverError = [[UIAlertView alloc] initWithTitle:#"Server error" message:#"Error connecting to page. If error persists, please contact support." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[serverError show];
webView.hidden = true;
//Otherwise load webView
} else {
// Redundant code
NSString *urlAddress = #"http://somesite.com/somepage";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
webView.hidden = false;
}
}
// Seems redundant since we are already checking the URLConnection
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
UIAlertView *connectionError = [[UIAlertView alloc] initWithTitle:#"Connection error" message:#"Error connecting to page. Please check your 3G and/or Wifi settings." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[connectionError show];
}
I guess what I'm wondering is, are there any shortcuts to achieve the desired functionality? Can I somehow access the URLResponse via the WebView directly? Does a nil value for the URLConnection or UIWebView imply connection errors without having to explicitly check for them? Is there an easier way to pass the URLRequest down into the delegate methods so it doesn't have be recreated twice?
Thanks in advance!
- (void)viewDidLoad {
webView = [[UIWebView alloc]init];
[webView setDelegate:self];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://yourUrl.com"]]]
[super viewDidLoad];
}
#pragma mark UIWebView delegate methods
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//read your request here
//before the webview will load your request
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
//access your request
webView.request;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
//access your request
webView.request;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(#"could not load the website caused by error: %#", error);
}
-(void)dealloc{
[webView release];
[super dealloc];
}
You can load the webpage with NSURLRequest directly into your webview. With the delegate callback methods you can change the behavior of your webview.
With this piece of code you should get it done.

Stopping network activity indicator

I used the below code to load a webpage. Everything works fine, but I want to stop the
network activity indicator after completing loading the webpage. How can we know that
the webpage is loaded completely.
Anyone please help.
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES; // to stop it, set this to NO
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
The simplest way to do this is to add this line after you instantiate your UIWebView.
[webView setDelegate:self];
Now you will call webViewDidFinishLoad: and the entire method should look like this.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
I'll explain this further since you will absolutely need to understand delegation in the future.
UIWebView is merely an object that displays web data. It doesn't really like handling all this other stuff, because it really just loves web data. Now in your case you wanted to find out when UIWebView was done doing its favorite little task. To do this, your UIWebView gives a shoutout to it's delegate like "Yo holmes, I'm done loading this data now. So go do whatever it is you do now." and your UIWebView continues on its merry way.
So what we did is we told the UIWebView that its delegate, in this case, was our current class by setting the delgate property on webView to self.
From there you can call any of the methods available to the UIWebView delegate (its all in the documentation) and have them perform tasks secondary to the main purpose of the UIWebView.
Make sense?
You just need to set webView.delegate to point to some object, and have that object implement webViewDidFinishLoad:.
To show network Indicator while UIWebView load request use below code..
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
//For Showing NetWork Indicator in Webview
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
Note: UIViewController<UIWebViewDelegate> and [webView setDelegate:self];
here's what I used to stop the activity indicator:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlAddress = [NSString stringWithFormat:#"http://www.wikipedia.com/wiki/%#", place.name];
NSURL *url= [NSURL URLWithString: [urlAddress stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[[self webView] setDelegate:self];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activityIndicator stopAnimating];
}
- (void) webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
}