1. 在webview读取网页中,在需要点击的url中添加onclick()事件
window.location="/alert"; //引号中可以改成自己想设置的字符
2.给本地webView添加 UIWebViewDelegate
添加如下方法:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.mainDocumentURL.relativePath isEqualToString:@"/alert"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"本地代码执行" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
return false; //执行本地代码,返回false不让网页读取网络资源
}
return true; //如没有location对应的属性,则读取网络相关资源
}