IOS中UIWebView、WKWebView之JS交互
做客戶端開發(fā),肯定避免不了JS交互,于是自己對(duì)蘋果接口做了個(gè)簡(jiǎn)易封裝:
JSExport-->UIWebView+Interaction、WKScriptMessageHandler -->WKWebView+Interaction以備以后使用。
代碼非常簡(jiǎn)潔,見這里:https://github.com/V5zhou/JSInteraction.git
舊方式
舊的交互方式有通過UIWebViewDelegate實(shí)現(xiàn)的:JS與客戶端定義好跳轉(zhuǎn)頁(yè)面參數(shù),在將要跳轉(zhuǎn)時(shí)捕獲關(guān)鍵字,然后處理業(yè)務(wù)。
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方式
導(dǎo)入JavaScriptCore.framework,并導(dǎo)入我的擴(kuò)展類#import "UIWebView+Interaction.h"。
使用方式
OC調(diào)JS:
[_webView InterActionToJs:@"alertMobile('15625298071')"];
JS調(diào)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;
}
}];
}
添加動(dòng)作
//自定義添加功能類型
typedef NS_ENUM(NSUInteger, InterActionOcType) {
InterActionOcType_alert = 0,
InterActionOcType_present,
InterActionOcType_xxxxxxx, //有啥需求就和這里添加
};
并且對(duì)應(yīng)的html中添加JS,參數(shù)封裝為字典形式。例:
function myPresent(ctl) {
var param = new Array();
param["animate"] = 1;
param["toController"] = "SecondViewController";
WebViewInteraction.callBack(1, param);
}
其中callBack是通過這個(gè)JSExport實(shí)現(xiàn)的
@protocol WebViewJSExport <JSExport> JSExportAs (callBack /** callBack 作為js方法的別名 */, - (void)awakeOC:(InterActionOcType)type param:(NSDictionary *)param ); @end
WKWebView+WKScriptMessageHandler方式
導(dǎo)入WebKit.framework,并導(dǎo)入我的擴(kuò)展類#import "WKWebView+Interaction.h"。
使用方式
OC調(diào)JS:
[self.wkWebView InterActionToJs:@"JSReloadTitle('你點(diǎn)了刷新JS按鈕,我沒猜錯(cuò)!')"];
JS調(diào)OC:
//注冊(cè)交互類型
[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;
}
}];
添加動(dòng)作
//自定義添加功能類型
typedef NS_ENUM(NSUInteger, WKInterActionOcType) {
WKInterActionOcType_alert = 0,
WKInterActionOcType_dismiss,
WKInterActionOcType_xxxxxxx, //有啥需求就和這里添加
};
并且對(duì)應(yīng)的html中添加JS,參數(shù)封裝為字典形式。例:
//js調(diào)oc
function myDismiss() {
window.webkit.messageHandlers.OCDismiss.postMessage({"animate" : 1}); //這里的OCDismiss對(duì)應(yīng)注冊(cè)類型
}
其中callBack是通過WKScriptMessageHandler實(shí)現(xiàn)的
- (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);
}
});
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS實(shí)現(xiàn)動(dòng)態(tài)的開屏廣告示例代碼
啟動(dòng)圖是在iOS開發(fā)過程中必不可少的一個(gè)部分,很多app在啟動(dòng)圖之后會(huì)有一張自定義的開屏廣告圖,但是有的時(shí)候需要讓啟動(dòng)圖看起來就是一個(gè)廣告,而且還要這個(gè)廣告里面會(huì)動(dòng),iOS的啟動(dòng)圖只能是靜態(tài)的,而且固定,為了實(shí)現(xiàn)看起來的動(dòng)畫效果,只能進(jìn)行偽造了。下面來一起看看2016-09-09
詳解關(guān)于iOS內(nèi)存管理的規(guī)則思考
本篇文章主要介紹了關(guān)于iOS內(nèi)存管理的規(guī)則思考,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12
iOS App使用GCD導(dǎo)致的卡頓現(xiàn)象及解決方法
這篇文章主要給大家介紹了關(guān)于iOS App使用GCD導(dǎo)致的卡頓現(xiàn)象及解決方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
禁止iPhone Safari video標(biāo)簽視頻自動(dòng)全屏的辦法
本篇文章給大家分析有沒有辦法禁止iPhone Safari video標(biāo)簽視頻自動(dòng)全屏,以下給出好多種情況分享,感興趣的朋友可以參考下2015-09-09
IOS 數(shù)據(jù)存儲(chǔ)詳解及實(shí)例代碼
這篇文章主要介紹了IOS 數(shù)據(jù)存儲(chǔ)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02

