還要寫文章嗎
JS核心代碼
- window.webkit.messageHandlers.JSCallOCMethod1.postMessage({"key1":"value1","key2":"value2"});
OC核心代碼
OC暴露方法給JS, 供JS調(diào)用
WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc] init];
WKUserContentController *wkUserController = [[WKUserContentController alloc] init];
NSArray *ocMeths = @[@"goDetail", @"goBack"];
for (NSString *ocMethString in ocMeths) {
[wkUController addScriptMessageHandler:self name:ocMethString];
}
webConfig.userContentController = wkUController;
WKWebView *atWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 100, ScreenW, ScreenH-100) configuration:webConfig];
處理js調(diào)用, JS調(diào)用OC
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
// 處理js的調(diào)用
NSLog(@"name:%@\\\\n body:%@\\\\n frameInfo:%@\\\\n",message.name,message.body,message.frameInfo);
}