UIWebView 点击网页URL调用app 本地代码

原创
2012/11/28 15:49
阅读数 8.6K

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对应的属性,则读取网络相关资源
}


展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
5 收藏
0
分享
返回顶部
顶部