我正在尝试从JSContext块创建一个webview。但是,它会导致新的webview加载没有其资产(css / js)的网页,我认为这可能是由于块的JSContext与webview的上下文之间的冲突所致。 / p>
在块中添加 那里发生了什么?
有没有办法让它在没有dispatch_after
- 这会延迟webview的初始化并可能摆脱JS上下文 - 完美地运行。let block: @objc_block (NSString!) -> Void = {
(string : NSString!) -> Void in
// This will produce a webview with the CSS that is not loading.
// A solution is to dispatch_after the following code so it's executed outside of the JS callback.
self.webview = UIWebView()
self.webview.frame = self.view.frame
self.view.addSubview(self.webview)
self.webview.loadRequest(NSURLRequest(URL: NSURL(string: "https://google.com")!))
/*
// Working hack
dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), { () -> Void in
self.webview = UIWebView()
self.webview.frame = self.view.frame
self.view.addSubview(self.webview)
self.webview.loadRequest(NSURLRequest(URL: NSURL(string: "https://google.com")!))
})
*/
}
let context = JSContext()
context.setObject(unsafeBitCast(block, AnyObject.self), forKeyedSubscript: "test")
context.evaluateScript("test()")
dispatch_after
黑客的情况下工作?