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

来源:互联网 发布:mc无线虚能矩阵 编辑:程序博客网 时间:2024/05/12 16:45

http://my.oschina.net/u/566401/blog/92659

1. 在webview读取网页中,在需要点击的url中添加onclick()事件

window.location="/alert";    //引号中可以改成自己想设置的字符

2.给本地webView添加 UIWebViewDelegate

添加如下方法:


01-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
02{
03    if ([request.mainDocumentURL.relativePath isEqualToString:@"/alert"]) {
04        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"本地代码执行" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
05        [alert show];
06        return false;  //执行本地代码,返回false不让网页读取网络资源
07    }
08     
09    return true;   //如没有location对应的属性,则读取网络相关资源
10}

0 0