雪蝠 發表於 2022-3-28 16:41:00

iOS开发 WKWebView实现JS交互

<p>需求:利用webView加载H5页面,并实现点击H5页面的按钮触发事件,事件是调用OC的方法。<br>简单说就是JS调用OC的方法,这里我们选择的是WKWebView,至于为什么不用UIWebView,自行百度或者看开发文档。<br>实现:<br>1、首先应该倒入 WebKit 框架 #import &lt;WebKit/WebKit.h&gt;<br><br>2、懒加载一个webView</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//<span style="color: rgba(0, 128, 0, 1)">需要的代理
&lt;WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler&gt;<span style="color: rgba(0, 0, 0, 1)">

@property (nonatomic , strong) WKWebView *<span style="color: rgba(0, 0, 0, 1)">webView;


- (WKWebView *<span style="color: rgba(0, 0, 0, 1)">)webView{
    <span style="color: rgba(0, 0, 255, 1)">if (!<span style="color: rgba(0, 0, 0, 1)">_webView) {
      WKWebViewConfiguration *config =<span style="color: rgba(0, 0, 0, 1)"> [init];
      _webView = [initWithFrame:CGRectMake(<span style="color: rgba(128, 0, 128, 1)">0, NAVHEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-<span style="color: rgba(0, 0, 0, 1)">NAVHEIGHT) configuration:config];<br>      if (@available(iOS 11.0, *)) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
      _webView.UIDelegate =<span style="color: rgba(0, 0, 0, 1)"> self;
      _webView.navigationDelegate =<span style="color: rgba(0, 0, 0, 1)"> self;
      NSURLRequest *request =<span style="color: rgba(0, 0, 0, 1)"> [initWithURL:];
      ;
    }

    <span style="color: rgba(0, 0, 255, 1)">return<span style="color: rgba(0, 0, 0, 1)"> _webView;

}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</div>
<p><br>3、JS端的方法构成HTML代码:<br><br>切记在html中预留接口,格式必须是固定的:</p>
<div class="cnblogs_code">
<pre>window.webkit.messageHandlers.actionName.postMessage(<span style="color: rgba(128, 0, 0, 1)">'<span style="color: rgba(128, 0, 0, 1)">parameter<span style="color: rgba(128, 0, 0, 1)">'<span style="color: rgba(0, 0, 0, 1)">);
actionName:为方法名
parameter:为传递过来的参数,在html中叫做对象,到OC我们这里就是字典</span></span></span></span></pre>
</div>
<p><br>举个例子:</p>
<div class="cnblogs_code">
<pre>window.webkit.messageHandlers.actionName.postMessage(<span style="color: rgba(128, 0, 0, 1)">'<span style="color: rgba(128, 0, 0, 1)">parameter<span style="color: rgba(128, 0, 0, 1)">'<span style="color: rgba(0, 0, 0, 1)">);
actionName:为方法名
parameter:为传递过来的参数,在html中叫做对象,到OC我们这里就是字典</span></span></span></span></pre>
</div>
<p>这里我们没有带参数,要是需要参数,就让前端加上,你不会没关系,也不需要你会,前端知道该怎么做。<br><br>4、OC端添加JS方法处理器:</p>
<div class="cnblogs_code">
<pre>WKUserContentController *userContentController =<span style="color: rgba(0, 0, 0, 1)"> _webView.configuration.userContentController;
      ;
      ;
      ;
      ;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</div>
<p>这里我们添加了了四个方法,切记着四个方法的名称无比与JS端一致。<br><br>5、实现WKScriptMessageHandler代理方法<br><br></p>
<div class="cnblogs_code">
<pre>- (<span style="color: rgba(0, 0, 255, 1)">void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *<span style="color: rgba(0, 0, 0, 1)">)message{
    NSLog(<span style="color: rgba(128, 0, 0, 1)">@"<span style="color: rgba(128, 0, 0, 1)">ScriptMessage 传递的消息内容 : %@<span style="color: rgba(128, 0, 0, 1)">"<span style="color: rgba(0, 0, 0, 1)">,message.body);
    <span style="color: rgba(0, 0, 255, 1)">if () {
    }
    <span style="color: rgba(0, 0, 255, 1)">if () {
    }
    <span style="color: rgba(0, 0, 255, 1)">if () {
    }
    <span style="color: rgba(0, 0, 255, 1)">if () {
    }
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</div>
<p>6、在界面消失的时候清除JS方法</p>
<div class="cnblogs_code">
<pre>- (<span style="color: rgba(0, 0, 255, 1)">void<span style="color: rgba(0, 0, 0, 1)">)viewWillDisappear:(BOOL)animated {
    ;
   <span style="color: rgba(0, 0, 0, 1)">
    ;
    ;
    ;<br>    ;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<pre><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)"> }</span></span></pre>
</div>
<p>到这里就结束了,当你点击 webView 上的按钮时,就能够进入我们的OC方法里。<br>总结起来就三点内容:<br>1.前端和苹果移动端的方法名必须一致,且前端要以固定的格式来写方法,否则移动端收不到事件反馈;<br>2.通过 WKUserContentController 添加html中预留的方法;<br>3.实现 WKScriptMessageHandler 的代理方法; </p><br><br>
来源:https://www.cnblogs.com/laolitou-ping/p/16067824.html
頁: [1]
查看完整版本: iOS开发 WKWebView实现JS交互