iOS webview 和 js 交互

来源:互联网 发布:布比网络 编辑:程序博客网 时间:2024/06/05 16:30

1.先上一段 html 代码

<html>

    <head>

        <meta http-equiv="Content-Type"content="text/html; charset=utf8">

    </head>

    <body>

       <h1>这是一段内容</h1>

        <input type="button"value="测试" onclick="check()" />

        <input type="button"value="测试2"onclick="check2()"/>

    </body>

</html>


2. js 代码

function loadURL(url) {

   var iFrame;

    iFrame = document.createElement("iframe");

    iFrame.setAttribute("src", url);

    iFrame.setAttribute("style","display:none;");

    iFrame.setAttribute("height","0px");

    iFrame.setAttribute("width","0px");

    iFrame.setAttribute("frameborder","0");

    document.body.appendChild(iFrame);

    //发起请求后这个iFrame就没用了,所以把它从dom上移除掉

    iFrame.parentNode.removeChild(iFrame);

    iFrame =null;

}

function check() {

    loadURL("lichongyang:abc");

}

function check2() {

    loadURL("lichongyang2:abc");

}


3.当js 调用 iOS 的时候 通过上面的js 代码 会重定向一个 请求的url  在

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

   NSURL * url = [request URL];

    if ([[urlabsoluteString] rangeOfString:@"abc"].location!=NSNotFound) {

        UIAlertView * alertView = [[[UIAlertViewalloc] initWithTitle:@"test"message:[url absoluteString]delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil]autorelease];

        [alertViewshow];

       return NO;

    }

    

    //获取当前页面的title

    //self.title   [webView stringByEvaluatingJavaScriptFromString:@"document.title"];

    return YES;

}







0 0
原创粉丝点击