0

I am trying to intercept a URL change when using a UIWebView. I currently open a URL (http://www.google.com). However, I would like to know when the user has changed the URL by clicking any of the buttons on the Google webpage. I would then like to do something before the new page loads.

I have tried:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

however, it does not seem to get called when the URL changes. I do not receive any log in my console, and it loads the page even if the bool returned is "NO".

I have also tried:

- (void)webViewDidStartLoad:(UIWebView *)webView

However, that selector doesn't seem to be called either. Would anyone know of a way to detect a URL change and do something before the new site is loaded? My code is below:

@synthesize webView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {

    NSString *urlAddress = @"http://www.google.com";

    //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];

    UIView *tabBar = [self.navigationController.view.superview viewWithTag:100];
    tabBar.hidden = YES;
    self.navigationController.view.frame = RECT_PERSONETICS_FRAME;

}

- (void)webViewDidFinishLoad {
    // Other stuff if necessary...

    // Could use hidden instead of enabled if you don't even want
    // the button to be visible
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"Webview URL: %@",[[[webView request] URL] absoluteString]);
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"New URL is: %@", request);
    return NO;
}
4

1 回答 1

2

由于没有调用方法,我假设您可能没有在您的UIWebViewDelegate中设置viewController或您的webview插座设置不正确。

于 2013-04-04T14:57:53.197 回答