2013-04-04 122 views
0

我试图在使用UIWebView时拦截URL更改。我目前打开一个URL(http://www.google.com)。不过,我想知道用户何时通过点击Google网页上的任何按钮来更改网址。然后我想在新页面加载之前做一些事情。UIWebView URL拦截

我曾尝试:

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

但是,它似乎并没有被调用时,URL变化。我没有收到我的控制台中的任何日志,并且即使返回的布尔值为“否”,它也会加载页面。

我也曾尝试:

- (void)webViewDidStartLoad:(UIWebView *)webView 

然而,选择似乎并没有被任何调用。 有人会知道一种方法来检测URL的变化,并在新网站加载之前做一些事情吗?我的代码如下:

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

你设置了webview的委托吗?它没有在您发布的代码中设置。 – ChrisH 2013-04-04 14:54:29

+0

糟糕,是的,这是问题所在。我不能相信我忘了这么做。谢谢! – 2013-04-04 14:57:28

回答

2

由于没有被调用的方法,我认为你可能没有设置UIWebViewDelegateviewController或您的webview出口设置不正确。