這篇文章主要為大家展示了“如何使用WKWebView、WebView和JS實現交互”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“如何使用WKWebView、WebView和JS實現交互”這篇文章吧。
由于Xcode8發布之后,編譯器開始不支持iOS 7了,這樣我們的app也改為最低支持iOS 8.0,既然需要與web交互,那自然也就選擇使用了 iOS 8.0之后 才推出的新控件 WKWebView.
相比與 UIWebView, WKWebView 存在很多優勢:
支持更多的HTML5的特性
高達60fps滾動刷新頻率與內置手勢
與Safari相容的JavaScript引擎
在性能、穩定性方面有很大提升占用內存更少 協議方法及功能都更細致
可獲取加載進度等。
UIWebView與JS的交互方式
一,OC調用JS
直接調用蘋果提供的API
- (nullable NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;
使用方式:
OC部分:
[self.webView stringByEvaluatingJavaScriptFromString:@"add(1,2)"];
JS部分:
function add(a,b) {
return a+b;
}二,JS調用OC
OC處理JS的時機在UIWebView的代理方法內
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
使用方式:
JS部分:
function btnClick1() {
location.href = "jsCallBack://method_?param1¶m2"
}OC部分:
NSString *schem = webView.request.URL.scheme;
if ([schem containsString:@"jsCallBack://"]) {
//action...
return NO;
}WKWebView與JS的交互方式
一,OC調用JS
調用蘋果提供的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調用OC
OC部分:
這種使用方式比較麻煩一些
1.在創建wkWebView時,需要將被js調用的方法注冊進去
//創建WKWebViewConfiguration文件 WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; config.preferences.minimumFontSize = 10.f; [config.userContentController addScriptMessageHandler:self name:@"playSound"]; //創建WKWebView類 WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
2.在WKScriptMessageHandler代理方法中監聽js的調用
#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調用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調用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.首先定義一個協議,這個協議遵守JSExport協議
@protocol JSExportTest <JSExport> @property (nonatomic, assign) NSInteger sum; JSExportAs(add, - (NSInteger)add:(int)a b:(int)b); @end
其中JSExportAs()是系統提供的宏,用來聲明在JS環境中方法add與OC環境中方法- (NSInteger)add:(int)a b:(int)b對應。
2.創建一類,遵守JSExportTest協議,并實現它什么的方法與屬性
@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;
}
@end3.使用方式:
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)"];這三種使用方式可以根據實際情況進行適當使用
以上是“如何使用WKWebView、WebView和JS實現交互”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。