iOS webview 和 js 交互

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 ([[url absoluteString] rangeOfString:@"abc"].location!= NSNotFound) {

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

        [alertView show];

        return NO;

    }

    

    //获取当前页面的title

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

    return YES;

}







  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值