Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView back button implementation issue in iPad

I have implemented a browser in my application by using UIWebView, by default I'm loading google page in my browser.

When I search something in the google page ,the UIWebViewDelegate's webView:shouldStartLoadWithRequest:navigationType: method is called.

The problem is when I tap on the back button from this search page no delegates are getting called, so I am having a problem disabling my back button.

This problem happens only in an iPad application not in an iPhone application.

like image 919
Shaik Riyaz Avatar asked Jan 14 '23 21:01

Shaik Riyaz


2 Answers

This code may help u...

A UIWebView is a UIView that can load a web page while remaining in the user's application. Navigation to other webpages is allowed through the use of imbedded links in a web page itself. Forward and backward navigation through history can be set up with instance methods goForward and goBack, but the programmer must supply the buttons.

The following example uses a UIWebView, and

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

2) adds a UIActivityIndicatorView which displays while the web page is loading

In the .h file for the WebViewController :

Declare the UIWebView, Optionally : add buttons to control moving forward and backward through browsing history and IBActions for pressing the buttons, Optionally again : add a 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 the 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
like image 152
iCrazyDev Avatar answered Jan 19 '23 11:01

iCrazyDev


In your case ,You have to ignore/avoid "caching data". Following lines of code may help.

NSURLRequest *requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"] cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0];
[webView loadRequest:requestObj];
like image 43
Muhammad Idris Avatar answered Jan 19 '23 13:01

Muhammad Idris