我一直在尝试将WKWebView集成为UIViewController中另一个webview的子视图。我已经能够加载内容并与Swift和JavaScript进行正确的通信。然而,没有任何数据操作的HTML内容加载时间接近2秒。
我还测试了在不加载任何脚本的情况下只加载空HTML体的WKWebView。这仍然需要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")
}
}
1条答案
按热度按时间ccrfmcuu1#
我认为有两种方法。
1.优化HTML、CSS和JavaScript代码。
2.您需要实现WKWebView委托函数。并且您需要在加载Webview内容后显示Webview内容。