欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WKWebView、WebView和JS的交互方式詳解

 更新時間:2018年04月21日 11:15:15   作者:滴水微瀾  
這篇文章主要給大家介紹了關(guān)于WKWebView、WebView和JS的交互方式,文中通過示例代碼介紹的非常詳細,對各位iOS開發(fā)者們具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。

由于Xcode8發(fā)布之后,編譯器開始不支持iOS 7了,這樣我們的app也改為最低支持iOS 8.0,既然需要與web交互,那自然也就選擇使用了 iOS 8.0之后 才推出的新控件 WKWebView.

相比與 UIWebView, WKWebView 存在很多優(yōu)勢:

  • 支持更多的HTML5的特性
  • 高達60fps滾動刷新頻率與內(nèi)置手勢
  • 與Safari相容的JavaScript引擎
  • 在性能、穩(wěn)定性方面有很大提升占用內(nèi)存更少 協(xié)議方法及功能都更細致
  • 可獲取加載進度等。

UIWebView與JS的交互方式

一,OC調(diào)用JS

直接調(diào)用蘋果提供的API

- (nullable NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;

使用方式:

OC部分:

[self.webView stringByEvaluatingJavaScriptFromString:@"add(1,2)"];

 JS部分:

function add(a,b) {
 return a+b;
}

二,JS調(diào)用OC

OC處理JS的時機在UIWebView的代理方法內(nèi)

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;

使用方式:

JS部分:

function btnClick1() {
 location.href = "jsCallBack://method_?param1&param2"
}

 OC部分:

NSString *schem = webView.request.URL.scheme;
 if ([schem containsString:@"jsCallBack://"]) {
 //action...
 return NO;
 }

WKWebView與JS的交互方式

一,OC調(diào)用JS

調(diào)用蘋果提供的API

- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;

使用方式:

OC部分:

[self.wkWebView evaluateJavaScript:@"playSount()" completionHandler:nil];

 JS部分:

function playSount() {
 //playSount...
}

二,JS調(diào)用OC

OC部分:

這種使用方式比較麻煩一些

1.在創(chuàng)建wkWebView時,需要將被js調(diào)用的方法注冊進去

//創(chuàng)建WKWebViewConfiguration文件
 WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
 config.preferences.minimumFontSize = 10.f;
 [config.userContentController addScriptMessageHandler:self name:@"playSound"];
//創(chuàng)建WKWebView類
 WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];

 2.在WKScriptMessageHandler代理方法中監(jiān)聽js的調(diào)用

#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
 
 if ([message.name isEqualToString:@"playSound"]) {
  [self playSound];
 }
}

 JS部分:

//JS響應事件
function btnClick() { window.webkit.messageHandlers.playSound.postMessage(null);
}

利用JavaScriptCore庫,WebView與JS的交互

一,OC調(diào)用JS

self.jsContent = [[JSContext alloc] init];
 
NSString *js = @"function add(a,b) {return a + b}";
[self.jsContent evaluateScript:js];
JSValue *jsValue = [self.jsContent[@"add"] callWithArguments:@[@2,@3]];

二,JS調(diào)用OC

self.jsContent = [[JSContext alloc] init];
self.jsContent[@"add"] = ^(int a, int b){
 NSLog(@"a+b = %d",a+b);
};
 
[self.jsContent evaluateScript:@"add(10,20)"];

三,JS直接訪問OC對象方法與屬性

1.首先定義一個協(xié)議,這個協(xié)議遵守JSExport協(xié)議

@protocol JSExportTest <JSExport>
@property (nonatomic, assign) NSInteger sum;
JSExportAs(add, - (NSInteger)add:(int)a b:(int)b);
 
@end

其中JSExportAs()是系統(tǒng)提供的宏,用來聲明在JS環(huán)境中方法add與OC環(huán)境中方法- (NSInteger)add:(int)a b:(int)b對應。

2.創(chuàng)建一類,遵守JSExportTest協(xié)議,并實現(xiàn)它什么的方法與屬性

@interface JSProtolObj : NSObject <JSExportTest>
@end
@implementation JSProtolObj
@synthesize sum = _sum;
- (NSInteger)add:(int)a b:(int)b {
 return a+b;
}
 
- (void)setSum:(NSInteger)sum {
 _sum = sum;
}
@end

3.使用方式:

self.jsContent = [[JSContext alloc] init];
self.jsContent.exceptionHandler = ^(JSContext *context, JSValue *exception) {
 [JSContext currentContext].exception = exception;
 NSLog(@"exception:%@",exception);
};
 
self.jsContent[@"OCobj"] = self.jsProtolObj;
[self.jsContent evaluateScript:@"OCobj.sum = OCobj.add(10,20)"];

這三種使用方式可以根據(jù)實際情況進行適當使用

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論