我一直在尝试将WKWebView集成为UIViewController中另一个webview的子视图。我已经能够加载内容并与swift和javascript正确通信。但是,没有任何数据操作的HTML内容的加载时间大约需要2秒。
我还测试了加载WKWebView只有一个空的HTML体,没有任何脚本加载。这仍然需要600毫秒才能加载。
viewDidLoad 和 webView(webView:WKWebView,didFinishNavigation导航:WKNavigation!)之间的时间差异是600毫秒,即使HTML包含空体。
ViewController的viewDidLoad函数
override func viewDidLoad() {
NSLog("started - %@.%@", String(self.dynamicType), __FUNCTION__)
super.viewDidLoad()
let wkConfiguration: WKWebViewConfiguration = WKWebViewConfiguration()
let userController: WKUserContentController = WKUserContentController()
wkConfiguration.userContentController = userController
wkConfiguration.processPool = VCWKWebView.wkProcess
self.wkWebView = VCWKWebView(frame: self.webView.bounds,configuration: wkConfiguration)
if let wkWebView = self.wkWebView {
self.webView.addSubview(wkWebView)
wkWebView.translatesAutoresizingMaskIntoConstraints = false
let height = NSLayoutConstraint(item: wkWebView, attribute: .Height, relatedBy: .Equal, toItem: self.webView, attribute: .Height, multiplier: 1, constant: 0)
let width = NSLayoutConstraint(item: wkWebView, attribute: .Width, relatedBy: .Equal, toItem: self.webView, attribute: .Width, multiplier: 1, constant: 0)
webView.addConstraints([height, width])
//wkWebView.delegate = self
wkWebView.navigationDelegate = self
wkWebView.loadContent()
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
NSLog("ended - %@.%@", String(self.dynamicType), __FUNCTION__)
}
VCWKWebView类:
class VCWKWebView: WKWebView {
static let wkProcess: WKProcessPool = WKProcessPool()
private static let _url: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("index", ofType: "html",inDirectory:"www")!)
private static let _accessUrl: NSURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("www", ofType: nil)!)
func loadContent(){
NSLog("started - %@.%@", String(self.dynamicType), __FUNCTION__)
if #available(iOS 9.0, *) {
self.loadFileURL(VCWKWebView._url, allowingReadAccessToURL: VCWKWebView._accessUrl)
} else {
// Fallback on earlier versions
}
NSLog("ended - %@.%@", String(self.dynamicType), __FUNCTION__)
}
override init(frame: CGRect, configuration: WKWebViewConfiguration) {
super.init(frame:frame, configuration:configuration)
}
convenience init(frame: CGRect){
let wkConfiguration: WKWebViewConfiguration = WKWebViewConfiguration()
self.init(frame:frame,configuration:wkConfiguration)
}
deinit{
print("deinit of VCWKWebView is called")
}
}
有人可以帮我集成WKWebView,以便内容加载更快吗?