我正在加载一个本地文件:
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"about" ofType:@"html"]isDirectory:NO]]];
并尝试在 Safari 中使用以下命令打开此文件中的任何外部链接:
-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
} else
return YES;
}
但这永远不会被调用。阅读我可以看到 shouldStartLoadWithRequest 不会被本地文件调用,但是在打开外部链接时应该调用它?
添加 web.delegate = self;
viewDidLoad 修复了它。哎呀