在WKWebView

时间:2016-10-19 12:06:58

标签: javascript ios uiwebview

我在一个原生的Objective-C库上工作,我将一些API暴露给JavaScript。目的是让它在Android和iOS上都可访问

在iOS上,我使用JSContext和JSExport as described here

目标C

 @import JavaScriptCore;


@protocol PersonExports <JSExport>

+ (void)sayHello:(NSString *)name;

@end

@interface Person : NSObject

+ (void)sayHello:(NSString *)name;

@end

然后我在完成加载时将公开的对象添加到UIWebView的JSContext中:

JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

if (nil != context) {
    context[@"Person"] = [Person class];
}

在此之后,我可以从加载页面中的JavaScript访问该对象并调用其上的方法:

的JavaScript

<script type="text/javascript">

Person.sayHello(@"Mickey");

</script>

问题是如何在WKWebView

中实现这一目标

1 个答案:

答案 0 :(得分:0)

// Create a WKWebView instance
self.webView = [[WKWebView alloc]initWithFrame:self.view.frame configuration:self.webConfig];

// Delegate to handle navigation of web content
self.webView.navigationDelegate = self;

[self.view addSubview:self.webView];

设置self.webConfig的访问者

#pragma mark - accessors
-(WKWebViewConfiguration*) webConfig {
if (!_webConfig) {
    // Create WKWebViewConfiguration instance
    _webConfig = [[WKWebViewConfiguration alloc]init];

    // Setup WKUserContentController instance for injecting user script
    WKUserContentController* userController = [[WKUserContentController alloc]init];

    // Add a script message handler for receiving  "buttonClicked" event notifications posted from the JS document using window.webkit.messageHandlers.buttonClicked.postMessage script message
    [userController addScriptMessageHandler:self name:@"buttonClicked"];

    // Get script that's to be injected into the document
    NSString* js = [self buttonClickEventTriggeredScriptToAddToDocument];

    // Specify when and where and what user script needs to be injected into the web document
    WKUserScript* userScript = [[WKUserScript alloc]initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO];

    // Add the user script to the WKUserContentController instance
    [userController addUserScript:userScript];

    // Configure the WKWebViewConfiguration instance with the WKUserContentController
    _webConfig.userContentController = userController;

}
return _webConfig;

}

从协议

实现脚本消息处理程序方法
#pragma mark -WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message     {
if ([message.name isEqualToString:@"buttonClicked"]) {
    self.buttonClicked ++;
}

// JS objects are automatically mapped to ObjC objects
id messageBody = message.body;
if ([messageBody isKindOfClass:[NSDictionary class]]) {
    NSString* idOfTappedButton = messageBody[@"ButtonId"];
    [self updateColorOfButtonWithId:idOfTappedButton];
}

}

并发布消息格式js,如此

var button = document.getElementById("clickMeButton");
button.addEventListener("click", function() {
    var messgeToPost = {'ButtonId':'clickMeButton'};
    window.webkit.messageHandlers.buttonClicked.postMessage(messgeToPost);
},false);

这对你有帮助,因为我在设置上下文nad调用方法表单JSExport

时也遇到了这个问题