JSContext加载触发器

时间:2018-10-29 13:59:08

标签: swift javascriptcore

我需要将这一系列代码从Objective-c重写为swift:

    JSContext *jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
jsContext[@"loaded"] = ^{
    NSLog(@"Content Loaded!");
};

问题是关于在加载webView时获取上下文。

1 个答案:

答案 0 :(得分:0)

找到了快速解决方案:

 if let jsContext = svtwebView?.value(forKeyPath: "documentView.webView.mainFrame.javaScriptContext") as? JSContext {
                let loaded: @convention(block) () -> Void = {
                    print("Loaded:")
                }
                jsContext.setObject(loaded, forKeyedSubscript: "loaded" as NSCopying & NSObjectProtocol)
                jsContext.evaluateScript("loaded")
            }