UIWebView URL拦截

时间:2013-04-04 14:36:51

标签: ios uiwebview

我正在尝试在使用UIWebView时拦截网址更改。我目前打开一个网址(http://www.google.com)。但是,我想知道用户何时通过点击Google网页上的任何按钮更改了网址。我想在新页面加载之前做点什么。

我试过了:

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

但是,当URL更改时,它似乎没有被调用。我没有在我的控制台中收到任何日志,即使返回的bool是" NO"它也会加载页面。

我也尝试过:

- (void)webViewDidStartLoad:(UIWebView *)webView

然而,这个选择器似乎也没有被调用。 是否有人知道检测网址更改的方法并在加载新网站之前执行某些操作?我的代码如下:

@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;
}

1 个答案:

答案 0 :(得分:2)

由于未调用方法,我认为您可能未在viewController中设置UIWebViewDelegate,或者webview插座未正确设置。