2013-07-04 94 views
0

我正在创建一个具有webview的应用程序。当用户点击一个链接或webview中的按钮,我想能够获得新的网址,并编辑新的网址。shouldStartLoadWithRequest委托方法从未调用

shouldStartLoadWithRequest应该做的伎俩,但是当我点击链接时,该方法从未被调用。我找不到为什么这不起作用。

我读的地方,我需要加入这一行:

webView.delegate = (id)self; 

我试了一下,仍然得到同样的问题。请帮助

AppDelegate.m

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    NSString *currentURL = [[request URL] absoluteString] ; 

    NSLog(@"Url: %@", currentURL); 
    return YES; 
} 

Controller.m或者

- (void)viewDidLoad 
{ 
    webView.delegate = (id)self; 
    NSString *tokenString = @"123"; 
    [super viewDidLoad]; 

    NSString *fullURL = [[NSString alloc] initWithFormat:@"http://192.168.1.69:8888/webapp/test.php?uid=%@", tokenString]; 
    NSURL *url = [NSURL URLWithString:fullURL]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [webView loadRequest:requestObj]; 
} 

回答

1

如果您Controller例如增加了自己作为代表,那么你添加到AppDelegate实例的方法永远不会被调用。

shouldStartLoadWithRequest方法移动到您的Controller类中。

+0

omg谢谢你我花了整整一天的时间! RRRRRR – user2037696

0

您已将视图控制器设置为Web视图的委托。但是,您已经在应用程序的委托中实现了UIWebView委托方法。

尝试在视图控制器中实现该委托方法。您的视图控制器是您的Web视图的代表。

相关问题