Reputation: 850
I have been trying to integrate the WKWebView
as a subview of another webview
in a UIViewController. I've been able to load the content and communicate properly with the swift
and JavaScript
. However, the loading time of the HTML
content without any data manipulation takes close to 2 seconds.
I have also tested loading the WKWebView
with just an empty body of HTML
without any scripts loading. This still takes 600 milliseconds to load.
The time taken difference between viewDidLoad
and the webView
(webView
: WKWebView
, didFinishNavigation navigation: WKNavigation!)** Is 600 ms, if even the HTML contains an empty body.
viewDidLoad function of the 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")
}
}
Upvotes: 58
Views: 3429
Reputation: 62
This usually happens because of CSS used in rendering web page. It is default behavior when loading page locally. We can also consider that in first load, UIWebview doesn't have cache to this and create cache for that page.
Please share your html part as well so I could check
Upvotes: 1