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

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

window.location="/alert";    //引号中能够改为本身想设置的字符 网络

2.给本地webView添加 UIWebViewDelegate url

添加以下方法: spa


-(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对应的属性,则读取网络相关资源
}
相关文章
相关标签/搜索