我正在创建一个具有 WebView 的应用程序。当用户单击 Web View 中的链接或按钮时,我希望能够获取新的 url 并编辑新的 url。
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];
}
最佳答案
如果您的 Controller
实例将自身添加为委托(delegate),那么您添加到 AppDelegate
实例的方法将永远不会被调用。
将 shouldStartLoadWithRequest
方法移至 Controller
类中。
关于iphone - 从未调用过 shouldStartLoadWithRequest 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17477063/