IOS中UIWebView, WKWebView之JS交互

  

做客户端开发,肯定避免不了JS交互,于是自己对苹果接口做了个简易封装:
  

  

JSExport——祝辞UIWebView +互动,WKScriptMessageHandler——祝辞WKWebView +交互以备以后使用。
  

  

代码非常简洁,见这里:https://github.com/V5zhou/JSInteraction.git

  

<强>旧方式

  

旧的交互方式有通过UIWebViewDelegate实现的:JS与客户端定义好跳转页面参数,在将要跳转时捕获关键字,然后处理业务。
  

  iOS端:

        (保龄球)webView:(UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *)请求navigationType: (UIWebViewNavigationType) navigationType {   NSString * urlString=[[请求URL] absoluteString];   如果([urlString isEqualToString: @ " objc://加载"]){   如果(_gotoRootViewController) {   _gotoRootViewController ();   }   }   返回YES;   }      

JS端:         & lt; !DOCTYPE html>   & lt; html>   & lt; title> test   & lt;元charset=皍tf - 8”比;   & lt; body>   & lt; a href=" javascript:文档。位置=objc://加载”rel="外部nofollow”class=" btn”在这是交互按钮& lt;/a>   & lt;/body>   & lt;/html>   之前      

<强> UIWebView + JSExport方式

  

导入JavaScriptCore.framework,并导入我的扩展类#导入“UIWebView + Interaction.h”。

  

<强>使用方式

  

OC调JS:

        [_webView InterActionToJs: @”alertMobile (' 15625298071 '));      JS调OC:

        - (void) webViewDidFinishLoad: (UIWebView *) webView {   (自我。webView InterActionToOc: ^ (InterActionOcType functionType NSDictionary * param) {   开关(functionType) {   案例InterActionOcType_alert:   {   UIAlertView *警报=[[UIAlertView alloc] initWithTitle: param[@“标题”]消息:param(@“内容”)委托:nil cancelButtonTitle: nil otherButtonTitles: @”确定”,nil);   (警报显示);   }   打破;   案例InterActionOcType_present:   {   自我。modalTransitionStyle=UIModalTransitionStyleCrossDissolve;   类Cls=NSClassFromString (param[@ "控制"]);   BOOL isAnimate=[参数(@“动画”)boolValue);   ui * ctl=[(Cls alloc) init);   【自我presentViewController: ctl动画:isAnimate完成:nil);   }   打破;      默认值:   打破;   }   });   }      之前      

<强>添加动作

     //自定义添加功能类型   typedef NS_ENUM(了NSUInteger InterActionOcType) {   InterActionOcType_alert=0,   InterActionOcType_present,   InterActionOcType_xxxxxxx,//有啥需求就和这里添加   };      

并且对应的html中添加JS,参数封装为字典形式。例:

        函数知道(ctl) {   var参数=new Array ();   参数(“动画”)=1;   参数(“控制”)=癝econdViewController”;   WebViewInteraction。回调(参数);   }      

其中回调是通过这个JSExport实现的

        @protocol WebViewJSExport & lt; JSExport>   JSExportAs   (回调/* *调作为js方法的别名*/- (void) awakeOC: (InterActionOcType)类型参数:参数(NSDictionary *)   );   @end      

<强> WKWebView + WKScriptMessageHandler方式

  

导入WebKit.framework,并导入我的扩展类#导入“WKWebView + Interaction.h”。

  

<强>使用方式

  

OC调JS:

        (自我。wkWebView InterActionToJs: @”JSReloadTitle(“你点了刷新JS按钮,我没猜错!”)“];      JS调OC:

     //注册交互类型   (自我。“OCDismiss”wkWebView registerScriptTypes: @ {@: @ (WKInterActionOcType_dismiss),   @“OCShowAlert”: @ (WKInterActionOcType_alert)});      (自我。wkWebView InterActionToOc: ^ (WKInterActionOcType functionType NSDictionary * param) {   开关(functionType) {   案例WKInterActionOcType_dismiss:   {   BOOL isAnimate=[参数(@“动画”)boolValue);   (自我dismissViewControllerAnimated: isAnimate完成:nil);   }   打破;   案例WKInterActionOcType_alert:   {   UIAlertView *警报=[[UIAlertView alloc] initWithTitle: @“JS去做平方”信息:nil委托:自我cancelButtonTitle: @”取消”otherButtonTitles: @”确定”,nil);   警报。alertViewStyle=UIAlertViewStylePlainTextInput;   (警报显示);   }   打破;   默认值:   打破;   }   });      

IOS中UIWebView, WKWebView之JS交互