重新阅读您的描述,看起来您真正需要了解的是如何使用WKWebView重新实现Javascript/Objective-C桥.
我刚刚自己做了这件事,按照http://tetontech.wordpress.com/2014/07/17/objective-c-wkwebview-to-javascript-and-back/的教程和http://nshipster.com/wkwebkit/的信息
WKWebView在Javascript和Objective-C/Swift:WKScriptMessageHandler
之间有一种内置的通信方式.
首先,在视图控制器的标题中包括WebKit标题和WKScriptMessageHandler
协议:
#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
@interface ViewController : UIViewController <WKScriptMessageHandler>
@end
初始化WKWebView
时,需要使用脚本消息处理程序对其进行配置.你想怎么命名就怎么命名,但对我来说,为你的应用命名似乎很有意义.
WKWebViewConfiguration *theConfiguration =
[[WKWebViewConfiguration alloc] init];
[theConfiguration.userContentController
addScriptMessageHandler:self name:@"myApp"];
_theWebView = [[WKWebView alloc] initWithFrame:self.view.frame
configuration:theConfiguration];
[_theWebView loadRequest:request];
[self.view addSubview:_theWebView];
现在,执行userContentController:didReceiveScriptMessage:
.当你的webview收到一条消息时,它就会触发,这样它就完成了你之前使用webView:shouldStartLoadWithRequest:navigationType:
所做的工作.
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message {
NSDictionary *sentData = (NSDictionary *)message.body;
NSString *messageString = sentData[@"message"];
NSLog(@"Message received: %@", messageString);
}
现在,您可以接收来自Javascript的消息了.需要添加到Javascript中的函数调用如下:
window.webkit.messageHandlers.myApp.postMessage({"message":"Hello there"});