I have been working on integrating the WKWebView
as a subview of another webview
within a UIViewController. I've successfully loaded the content and established proper communication between swift
and JavaScript
. However, I've noticed that the loading time for the plain HTML
content without any data manipulation is close to 2 seconds.
Even when testing the loading of the WKWebView
with just an empty HTML
body without any scripts involved, it still takes around 600 milliseconds to load.
The time disparity between the viewDidLoad
function and the webView
(webView
: WKWebView
, didFinishNavigation navigation: WKNavigation!)** is 600 ms, even when the HTML only contains an empty body.
ViewController's viewDidLoad Function:
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")
}
}