国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS中UIWebView、WKWebView之JS交互

2020-07-26 02:47:41
字體:
來源:轉載
供稿:網友

做客戶端開發,肯定避免不了JS交互,于是自己對蘋果接口做了個簡易封裝:

JSExport-->UIWebView+Interaction、WKScriptMessageHandler -->WKWebView+Interaction以備以后使用。

代碼非常簡潔,見這里:https://github.com/V5zhou/JSInteraction.git

舊方式

舊的交互方式有通過UIWebViewDelegate實現的:JS與客戶端定義好跳轉頁面參數,在將要跳轉時捕獲關鍵字,然后處理業務。

iOS端:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {  NSString *urlString = [[request URL] absoluteString];  if ([urlString isEqualToString:@"objc://loading"]) {    if (_gotoRootViewController) {      _gotoRootViewController();    }  }  return YES;}

JS端:

<!DOCTYPE html><html>  <title>test</title>  <meta charset="utf-8">    <body>      <a href="javascript:document.location = 'objc://loading'" rel="external nofollow" class="btn">這是交互按鈕</a>    </body></html>

UIWebView+JSExport方式

導入JavaScriptCore.framework,并導入我的擴展類#import "UIWebView+Interaction.h"。

使用方式

OC調JS:

[_webView InterActionToJs:@"alertMobile('15625298071')"];

JS調OC:

- (void)webViewDidFinishLoad:(UIWebView *)webView {  [self.webView InterActionToOc:^(InterActionOcType functionType, NSDictionary *param) {    switch (functionType) {      case InterActionOcType_alert:      {        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:param[@"title"] message:param[@"content"] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];        [alert show];      }        break;      case InterActionOcType_present:      {        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;        Class Cls = NSClassFromString(param[@"toController"]);        BOOL isAnimate = [param[@"animate"] boolValue];        UIViewController *ctl = [[Cls alloc] init];        [self presentViewController:ctl animated:isAnimate completion:nil];      }        break;      default:        break;    }  }];}

添加動作

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

并且對應的html中添加JS,參數封裝為字典形式。例:

function myPresent(ctl) {      var param = new Array();      param["animate"] = 1;      param["toController"] = "SecondViewController";      WebViewInteraction.callBack(1, param);    }

其中callBack是通過這個JSExport實現的

@protocol WebViewJSExport <JSExport>JSExportAs(callBack /** callBack 作為js方法的別名 */, - (void)awakeOC:(InterActionOcType)type param:(NSDictionary *)param );@end

WKWebView+WKScriptMessageHandler方式

導入WebKit.framework,并導入我的擴展類#import "WKWebView+Interaction.h"。

使用方式

OC調JS:

[self.wkWebView InterActionToJs:@"JSReloadTitle('你點了刷新JS按鈕,我沒猜錯!')"];

JS調OC:

//注冊交互類型  [self.wkWebView registerScriptTypes:@{@"OCDismiss" : @(WKInterActionOcType_dismiss),                     @"OCShowAlert" : @(WKInterActionOcType_alert)}];  [self.wkWebView InterActionToOc:^(WKInterActionOcType functionType, NSDictionary *param) {    switch (functionType) {      case WKInterActionOcType_dismiss:      {        BOOL isAnimate = [param[@"animate"] boolValue];        [self dismissViewControllerAnimated:isAnimate completion:nil];      }        break;      case WKInterActionOcType_alert:      {        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"JS去做平方" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];        alert.alertViewStyle = UIAlertViewStylePlainTextInput;        [alert show];      }        break;      default:        break;    }  }];

添加動作

//自定義添加功能類型typedef NS_ENUM(NSUInteger, WKInterActionOcType) {  WKInterActionOcType_alert = 0,  WKInterActionOcType_dismiss,  WKInterActionOcType_xxxxxxx,   //有啥需求就和這里添加};

并且對應的html中添加JS,參數封裝為字典形式。例:

//js調ocfunction myDismiss() {  window.webkit.messageHandlers.OCDismiss.postMessage({"animate" : 1});  //這里的OCDismiss對應注冊類型}

其中callBack是通過WKScriptMessageHandler實現的

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {  dispatch_async(dispatch_get_main_queue(), ^{    NSString *name = message.name;    NSDictionary *value = message.body;    WKInterActionOcType type = [self.typeDict[name] integerValue];    if (self.block) {      self.block(type, value);    }  });}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 牙克石市| 沧源| 昌宁县| 乐山市| 万安县| 新宁县| 黄龙县| 黑水县| 洞头县| 顺义区| 开原市| 东明县| 佳木斯市| 左权县| 淳安县| 冀州市| 巴林右旗| 白山市| 甘泉县| 延吉市| 巴彦淖尔市| 岳普湖县| 安新县| 泸州市| 龙里县| 岱山县| 沙坪坝区| 东乡| 化州市| 文水县| 扬州市| 宁乡县| 龙陵县| 绥宁县| 澎湖县| 扎囊县| 稷山县| 曲阜市| 英吉沙县| 汉沽区| 延寿县|