我需要将这一系列代码从Objective-c重写为swift:
JSContext *jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
jsContext[@"loaded"] = ^{
NSLog(@"Content Loaded!");
};
问题是关于在加载webView时获取上下文。
答案 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")
}