di aplikasi saya, saya bermigrasi dari UIWebView ke WKWebView, bagaimana cara menulis ulang fungsi ini untuk WKWebView?
func webViewDidStartLoad(webView: UIWebView){}
func webViewDidFinishLoad(webView: UIWebView){}
dan
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
print("webview asking for permission to start loading")
if navigationType == .LinkActivated && !(request.URL?.absoluteString.hasPrefix("http://www.myWebSite.com/exemlpe"))!{
UIApplication.sharedApplication().openURL(request.URL!)
print(request.URL?.absoluteString)
return false
}
print(request.URL?.absoluteString)
lastUrl = (request.URL?.absoluteString)!
return true
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
print("webview did fail load with error: \(error)")
let testHTML = NSBundle.mainBundle().pathForResource("back-error-bottom", ofType: "jpg")
let baseUrl = NSURL(fileURLWithPath: testHTML!)
let htmlString:String! = "myErrorinHTML"
self.webView.loadHTMLString(htmlString, baseURL: baseUrl)
}
navigationDelegate
satuWKWebView
.Jawaban:
UIWebView => WKWebView Equivalent
Tentang
shouldStartLoadWithRequest
Anda dapat menulis:Dan untuk
didFailLoadWithError
:sumber
WKNavigationDelegate
(bukanWKUIDelegate
).Berikut adalah metode Objective-C untuk migrasi
1) shouldStartLoadWithRequest -> memutuskanPolicyForNavigationAction
Ingatlah untuk memanggil
decisionHandler
2) webViewDidStartLoad -> didStartProvisionalNavigation
3) webViewDidFinishLoad -> didFinishNavigation
4) didFailLoadWithError -> didFailNavigation
sumber
WKNavigationTypeLinkActivated
bukanUIWebViewNavigationTypeLinkClicked
?Migrasi UIWebView ke WKWebView, Swift 4 :
Setara dengan
shouldStartLoadWithRequest
:Setara dengan
webViewDidStartLoad
:Setara dengan
didFailLoadWithError
:Setara dengan
webViewDidFinishLoad
:sumber