iOS解析---WebView和js交互原理

来源:互联网 发布:雷曼2胜利大逃亡mac 编辑:程序博客网 时间:2024/06/04 20:06

       

原文地址:点击打开链接



目前在网上看到很多介绍IOS里面WebView和JavaScript交互的文章。如果你想用网页去写一个app,那么我推荐 你去用PhoneGap (它是一个用基于HTML,CSS和JavaScript的,创建移动跨平台移动应用程序的快速开发平台)。如果你网页只是你app的一部分那么还是要去了解一下它们之间交互的原理。


       目前github上已经有相对成熟的开源项目,例如 WebViewJavascriptBridge ,你可以直接利用现成的框架快速实现js交互。介绍bridge的文章挺多,这里分析一个介绍的比较全面的博客:My_Lord 。有兴趣的同学可以去转转~


       现在,我来为大家分析一下其中的原理。其实很简单,在IOS中,WebView和JavaScript直接的调用就是一个实例方法、一个回调代理:

       首先来看WebView调用js的方法:

    //调用特定的js方法    [_myWebView stringByEvaluatingJavaScriptFromString:@"myFunction('Hello,js!');"];

      其中myFunction()为你的html页面里边早已写好的js函数。至于穿不穿参数,看你的选择。


      接下来是js调用WebView的方法,原理就是利用WebView的代理方法:

- (BOOL)<span id="8_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="8_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FObjective%5Fc%2F318278%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=web&k0=web&k1=%BF%AA%D4%B4&k2=javascript&k3=%BF%AA%B7%A2%C6%BD%CC%A8&k4=%B4%FA%C0%ED&k5=app&sid=c1d1272fce2e94d1&ch=0&tu=u1807396&jk=f961445607926bba&cf=29&fv=15&stid=9&urlid=0&luki=2&seller_id=1&di=128" target="_blank" mpid="8" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">web</span></a></span>View:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{}

      这个代理将会在js调用document.location的时候相应。接下来我们要定义好它们之间的暗号。

        function sendCommand(cmd,param){            var url="testapp:"+cmd+":"+param;            document.location = url;        }

      我们定义以testapp:开头的url为我们的暗号。接下来是在代理里边识别该暗号:

#pragma <span id="4_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="4_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FObjective%5Fc%2F318278%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=mark&k0=mark&k1=ios&k2=web&k3=%BF%AA%D4%B4&k4=javascript&k5=%BF%AA%B7%A2%C6%BD%CC%A8&sid=c1d1272fce2e94d1&ch=0&tu=u1807396&jk=f961445607926bba&cf=29&fv=15&stid=9&urlid=0&luki=10&seller_id=1&di=128" target="_blank" mpid="4" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">mark</span></a></span> - UIWebViewDelegate- (BOOL)<span id="5_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="5_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FObjective%5Fc%2F318278%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=web&k0=web&k1=%BF%AA%D4%B4&k2=javascript&k3=%BF%AA%B7%A2%C6%BD%CC%A8&k4=%B4%FA%C0%ED&k5=app&sid=c1d1272fce2e94d1&ch=0&tu=u1807396&jk=f961445607926bba&cf=29&fv=15&stid=9&urlid=0&luki=2&seller_id=1&di=128" target="_blank" mpid="5" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">web</span></a></span>View:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{    static int count = 1;        NSString *requestString = [[request.URL absoluteString]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];;        NSArray *components = [requestString componentsSeparatedByString:@":"];        if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"test<span id="6_nwp" style="list-style: none; width: auto; height: auto; float: none;"><a target=_blank id="6_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2E350351%2Ecom%2Fbianchengyuyan%2FObjective%5Fc%2F318278%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=83045059_cpr&k=app&k0=app&k1=%B2%A9%BF%CD&k2=%CD%A8%D0%C5&k3=mark&k4=ios&k5=web&sid=c1d1272fce2e94d1&ch=0&tu=u1807396&jk=f961445607926bba&cf=29&fv=15&stid=9&urlid=0&luki=7&seller_id=1&di=128" target="_blank" mpid="6" style="list-style: none; padding: 0px 10px; margin: 0px; color: rgb(0, 182, 137); text-decoration: none;"><span style="list-style: none; color: rgb(0, 0, 255); width: auto; height: auto;">app</span></a></span>"]) {                //解析约定的指令        if([(NSString *)[components objectAtIndex:1] isEqualToString:@"alert"])        {            UIAlertView *alert = [[UIAlertView alloc]                                  initWithTitle:[NSString stringWithFormat:@"消息%d",count++] message:[components objectAtIndex:2]                                  delegate:self cancelButtonTitle:nil                                  otherButtonTitles:@"OK", nil];            [alert show];        }        return NO;    }    return YES;}

        大功告成!是不是很简单~   只要我们定义好了传递的暗号,我们就可以让双方相互“通信”了!
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机返回键和菜单键失灵怎么办 苹果5s指纹按键坏了怎么办 小米手机安卓系统耗电量大怎么办? 苹果5s充不进去电怎么办 苹果手机6s返回键失灵怎么办 本人被骗同时被利用骗了别人怎么办 京东取消订单后货到了该怎么办 京东电信日租卡流量顶置了怎么办 苹果6s进水后闪光灯不亮怎么办 华为手机情景义停车事项过期怎么办 拼多多付款后商品下架了怎么办 淘宝上买化妆品买到假货了怎么办 找苹果官网解id发票丢了怎么办 客人已交订金但要取消宴席怎么办 京东买的小米电视碎屏了怎么办 京东购买的电视碎屏了怎么办 淘宝上买手机不能用不给退怎么办 天猫申请退货退款卖家不处理怎么办 在淘宝买到货到付款的假苹果怎么办 跟朋友买手机买到假货怎么办 在淘宝网上买到不合格的产品怎么办 淘宝打假师打了我的店铺怎么办 收藏品公司关门跑路员工怎么办 客户快递签收后说货物短缺怎么办 京东商城买东西商家不换货怎么办 在商场买东西过几天就降价了怎么办 天猫买东西不退货不退款怎么办 买买8p美版的怎么办 京东金条银行卡被冻结还不了怎么办 在瑞士刚买的浪琴手表不走了怎么办 刚买的手表表镜有划痕 怎么办 唯品会上买的手表有质量问题怎么办 我买的对方材料没开票给我怎么办 给对方修完车车主不给发票怎么办 买苹果手机花呗额度不够怎么办 苹果手机用别人的手机卡激活怎么办 小米商城花呗分期额度不够怎么办 淘宝已经形成订单商家不发货怎么办 小米商城退款后又想买了怎么办 淘宝退货退款后不想退了怎么办 在转转的商品被屏了怎么办