WKWebView loads HTML content too slowly in iOS9 using fast

I am trying to integrate WKWebView as a subtitle of another web view into a UIViewController. I was able to download the content and communicate correctly with fast and javascript. However, loading the HTML content without any data manipulation takes about 2 seconds.

I also tested loading WKWebView only with an empty HTML body without loading scripts. It takes 600 milliseconds to download.

The time difference between viewDidLoad and webView (webView: WKWebView, didFinishNavigation navigation: WKNavigation!) Is 600 ms, even if the HTML contains an empty body.

viewDidLoad function for ViewController

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:

 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") } } 

Can someone help me integrate WKWebView so that content loads faster?

+5
source share

Source: https://habr.com/ru/post/1241806/


All Articles