由于Xcode8發(fā)布之后,編譯器開始不支持iOS 7了,這樣我們的app也改為最低支持iOS 8.0,既然需要與web交互,那自然也就選擇使用了 iOS 8.0之后 才推出的新控件 WKWebView.
相比與 UIWebView, WKWebView 存在很多優(yōu)勢:
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的時(shí)機(jī)在UIWebView的代理方法內(nèi)
- (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調(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時(shí),需要將被js調(diào)用的方法注冊進(jìn)去
//創(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響應(yīng)事件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.首先定義一個(gè)協(xié)議,這個(gè)協(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對應(yīng)。
2.創(chuàng)建一類,遵守JSExportTest協(xié)議,并實(shí)現(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ù)實(shí)際情況進(jìn)行適當(dāng)使用
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網(wǎng)的支持。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注