iOS WKWebView与JS交互

来源:互联网 发布:贝奥武夫结局知乎 编辑:程序博客网 时间:2024/05/29 03:16

用户操作交互

就是截获JS调用alert、confirm、prompt函数,来使用原生控件实现样式及操作,并将用户操作回调给JS

代理:WKUIDelegate

具体步骤:

1、设置代理

webView.UIDelegate = self;

2、完成代理方法

部分代理方法

// 在JS端调用alert函数时,会触发此代理方法。// JS端调用alert时所传的数据可以通过message拿到// 在原生得到结果后,需要回调JS,是通过completionHandler回调-(void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler;// JS端调用confirm函数时,会触发此方法// 通过message可以拿到JS端所传的数据// 在iOS端显示原生alert得到YES/NO后// 通过completionHandler回调给JS端-(void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler;// JS端调用prompt函数时,会触发此方法// 要求输入一段文本// 在原生输入得到文本内容后,通过completionHandler回调给JS- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler;

演示:

演示


自定义方法

就是截获JS调用自定义方法,实现某些功能

代理:WKScriptMessageHandler

具体步骤:

1、注册方法

[webView.configuration.userContentController addScriptMessageHandler:self name:@"copyWeiXinHao"];   // 复制微信号[webView.configuration.userContentController addScriptMessageHandler:self name:@"goToWeiXinApp"];   // 跳转微信App[webView.configuration.userContentController addScriptMessageHandler:self name:@"getCurrentContent"];   // 获取原生剪切板内容

2、实现WKScriptMessageHandler方法、注册的方法

// message.name为方法名// message.body为js传递的参数,是个id类型- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{    if ([message.name isEqualToString:@"copyWeiXinHao"]) {        NSString *wxh = message.body;        NSLog(@"微信号:%@",wxh);        [self copyWeiXinHao:wxh];    }    else if ([message.name isEqualToString:@"getCurrentContent"]) {        NSString *content = [self getCurrentContent];        NSString *promptCode = [NSString stringWithFormat:@"getCurrentWeiXinHao(\"%@\")",content];        // OC传递数据给JS        [self.webView.webView evaluateJavaScript:promptCode completionHandler:^(id _Nullable response, NSError * _Nullable error) {        }];    }    else if ([message.name isEqualToString:@"goToWeiXinApp"]) {        [self goToWeiXinApp];    }}// 复制微信号-(void)copyWeiXinHao:(NSString *)wxh{    [[UIPasteboard generalPasteboard] setString:wxh];}// 当前剪切板信息-(NSString*)getCurrentContent{    return [[UIPasteboard generalPasteboard] string];}// 跳转微信应用-(void)goToWeiXinApp{    NSURL *url = [NSURL URLWithString:@"weixin://"];    BOOL canOpen = [[UIApplication sharedApplication] canOpenURL:url];    if (canOpen){   //打开微信        [[UIApplication sharedApplication] openURL:url];    }else {        NSLog(@"您的设备尚未安装微信");    }}

3、移除监听

-(void)dealloc{    // 根据name移除    [self.webView.webView.configuration.userContentController removeScriptMessageHandlerForName:@"copyWeiXinHao"];//    [self.webView.webView.configuration.userContentController removeAllUserScripts]; // 移除所有}

补充:

1、JS传递OC

function 方法名() {    window.webkit.messageHandlers.方法名.postMessage('参数');}

2、OC传递JS

[webView evaluateJavaScript:@"方法名(参数)" completionHandler:^(id _Nullable response, NSError * _Nullable error) {}];

演示:

演示


补充:JS文件

<!doctype html><html>    <head>        <meta charset="UTF-8">            <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/>            <title>JSCallOC</title>            <style>                *                {                    //-webkit-tap-highlight-color: rgba(0,0,0,0);                    text-decoration: none;                }            html,body            {                -webkit-touch-callout: none;                /* prevent callout to copy image, etc when tap to hold */                -webkit-text-size-adjust: none;             /* prevent webkit from resizing text to fit */                -webkit-user-select: none;                  /* prevent copy paste, to allow, change 'none' to 'text' */            }            #div-a            {                background:#FBA;                color:#FFF;                border-radius: 25px 5px;            }                </style>            </head>    <body style="background:#CDE; color:#FFF">        <div id="div-a">            <center>                <br/>                <input type="button" value=alert onclick="alert('禁止操作');" />                <br/>                <br/>                <input type="button" value="confirm" onclick="confirm('确定提交吗?');" />                <br/>                <br/>                <input type="button" value="prompt" onclick="prompt('请输入姓名');" />                <br/>                <br/>                <input type="button" value="复制微信号" onclick="copyWeiXinHao();" />                <br/>                <br/>                <input type="button" value="跳转微信" onclick="goToWeiXinApp();" />                <br/>                <br/>                <input type="button" value="获取应用剪切板内容" onclick="getCurrentContent();" />                <br/>                <br/>                <a id="push" href="#" onclick="">                    xxx                </a>            </center>        </div>        <script type="text/javascript">            function copyWeiXinHao() {                window.webkit.messageHandlers.copyWeiXinHao.postMessage('476512340');            }            function goToWeiXinApp() {                window.webkit.messageHandlers.goToWeiXinApp.postMessage('');            }            function getCurrentContent() {                window.webkit.messageHandlers.getCurrentContent.postMessage('');            }            function getCurrentWeiXinHao(wxh) {                document.getElementById('push').innerText = wxh;            }            </script>    </body></html>

demo演示地址

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 网盘停止服务怎么办 大水兰叶子黄了怎么办 ck石英表不走了怎么办 汽车电工1000个怎么办 面粉发酵不起来怎么办 产权50年到期怎么办 产权40年到期怎么办 乐视电视内屏裂怎么办 两岁宝宝骂人怎么办 缺10万块钱怎么办 钻戒钻石掉了怎么办 如果整形失败了怎么办 谁能告诉我该怎么办 面对说谎的老公怎么办 年龄大眼皮松弛怎么办 做包皮失败了怎么办 有孩子离婚了怎么办 离婚都不要孩子怎么办 一个经常换工作怎么办 孩子总是说谎话怎么办 小孩子老爱撒谎怎么办 赌钱输了不甘心怎么办 一个蛋碎了怎么办 孩子爱撒谎该怎么办 孩子老撒谎 家长怎么办 小孩爱说谎话怎么办 孩子爱说谎话怎么办 小孩说慌该怎么办 孩子老爱撒谎怎么办 孩子爱撒谎怎么办措施 发现孩子说谎了怎么办 蓝牙耳机不开机怎么办 蓝牙耳机不充电怎么办 麦进水没声音怎么办 蓝牙图标不显示怎么办 手机搜不到蓝牙怎么办 汽车音响进土了怎么办 先天性骶椎隐裂怎么办 狼牙裂开了怎么办 头层牛皮开裂怎么办 木茶盘裂缝漏水怎么办