Issue with implementing UIWebView button on iPad

I implemented a browser in my application using UIWebView , by default I load the google page in my browser.

When I search for something on the google page, the UIWebViewDelegate webView:shouldStartLoadWithRequest:navigationType: method is called.

The problem is that when I click the back button from this search page, delegates are not called, so I have a problem with disabling my back button.

This issue only occurs in the iPad application, not in the iPhone application.

+4
source share
2 answers

This code can help you ...

UIWebView is a UIView that can load a web page while remaining in a user application. Navigation to other web pages is permitted using the built-in links on the web page itself. Moving back and forth through history can be configured using the goForward and goBack instance methods, but the programmer must provide buttons.

The following example uses a UIWebView and

1) adds buttons forward and backward. Buttons are enabled and highlighted using additional methods UIWebViewDelegate: webViewDidStartLoad: and webViewDidFinishLoad:

2) Adds a UIActivityIndicatorView, which is displayed during the loading of the web page.

In the .h file for WebViewController:

Declare a UIWebView, optional: add buttons to control forward and backward browsing history and IBActions to click buttons. Optional again: add UIActivityIndicatorView.

 @interface WebViewController : UIViewController <UIWebViewDelegate> { UIWebView *webView; UIButton *back; UIButton *forward; UIActivityIndicatorView *activityIndicator; } @property(nonatomic,retain)IBOutlet UIWebView *webView; @property(nonatomic,retain)IBOutlet UIButton *back; @property(nonatomic,retain)IBOutlet UIButton *forward; @property(nonatomic,retain)IBOutlet UIActivityIndicatorView *activityIndicator; -(IBAction)backButtonPressed: (id)sender; -(IBAction)forwardButtonPressed: (id)sender; @end 

// In the .m file for WebViewController

 @implementation WebViewController @synthesize webView; @synthesize back; @synthesize forward; @synthesize activityIndicator; //method for going backwards in the webpage history -(IBAction)backButtonPressed:(id)sender { [webView goBack]; } //method for going forward in the webpage history -(IBAction)forwardButtonPressed:(id)sender { [webView goForward]; } //programmer defined method to load the webpage -(void)startWebViewLoad { //NSString *urlAddress = @"http://www.google.com"; NSString *urlAddress = @"http://cagt.bu.edu/page/IPhone-summer2010-wiki_problemsandsolutions"; //Create a URL object. NSURL *url = [NSURL URLWithString:urlAddress]; //URL Requst Object NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; //Load the request in the UIWebView. [webView loadRequest:requestObj]; } // acivityIndicator is set up here - (void)viewDidLoad { //start an animator symbol for the webpage loading to follow UIActivityIndicatorView *progressWheel = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; //makes activity indicator disappear when it is stopped progressWheel.hidesWhenStopped = YES; //used to locate position of activity indicator progressWheel.center = CGPointMake(160, 160); self.activityIndicator = progressWheel; [self.view addSubview: self.activityIndicator]; [self.activityIndicator startAnimating]; [progressWheel release]; [super viewDidLoad]; //call another method to do the webpage loading [self performSelector:@selector(startWebViewLoad) withObject:nil afterDelay:0]; } - (void)dealloc { [webView release]; [back release]; [forward release]; [activityIndicator release]; [super dealloc]; } #pragma mark UIWebViewDelegate methods //only used here to enable or disable the back and forward buttons - (void)webViewDidStartLoad:(UIWebView *)thisWebView { back.enabled = NO; forward.enabled = NO; } - (void)webViewDidFinishLoad:(UIWebView *)thisWebView { //stop the activity indicator when done loading [self.activityIndicator stopAnimating]; //canGoBack and canGoForward are properties which indicate if there is //any forward or backward history if(thisWebView.canGoBack == YES) { back.enabled = YES; back.highlighted = YES; } if(thisWebView.canGoForward == YES) { forward.enabled = YES; forward.highlighted = YES; } } @end 

/ ******************************* /

 //In viewDidLoad for the class which adds the WebViewController: WebViewController *ourWebVC = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil]; ourWebVC.title = @"WebView"; [self.view addSubview:ourWebVC]; //release ourWebVC somewhere else 
+5
source

In your case, you should ignore / avoid "data caching". The following lines of code may help.

 NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"] cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]; [webView loadRequest:requestObj]; 
+4
source

All Articles