Necesitamos tu apoyo para extender nuestras reseñas en referencia a las ciencias informáticas.
Solución:
UIWebView => Equivalente a WKWebView
UIWebViewDelegate => WKNavigationDelegate
delegate => navigationDelegate
didFailLoadWithError => didFailNavigation
webViewDidFinishLoad => didFinishNavigation
webViewDidStartLoad => didStartProvisionalNavigation
shouldStartLoadWithRequest => decidePolicyForNavigationAction
Acerca de shouldStartLoadWithRequest
puedes escribir:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
print("webView:(webView) decidePolicyForNavigationAction:(navigationAction) decisionHandler:(decisionHandler)")
switch navigationAction.navigationType
case .linkActivated:
if navigationAction.targetFrame == nil
self.webView?.loadRequest(navigationAction.request)
if let url = navigationAction.request.url, !url.absoluteString.hasPrefix("http://www.myWebSite.com/example")
UIApplication.shared.open(url)
print(url.absoluteString)
decisionHandler(.cancel)
return
default:
break
if let url = navigationAction.request.url
print(url.absoluteString)
decisionHandler(.allow)
Y para el didFailLoadWithError
:
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error)
print("webView:(webView) didFailNavigation:(navigation) withError:(error)")
let testHTML = Bundle.main.path(forResource: "back-error-bottom", ofType: "jpg")
let baseUrl = URL(fileURLWithPath: testHTML!)
let htmlString = "myErrorInHTML"
self.webView.loadHTMLString(htmlString, baseURL: baseUrl)
Aquí está el C objetivo métodos para el migración
1) shouldStartLoadWithRequest -> decidePolicyForNavigationAction
Recuerda llamar al decisionHandler
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
if (navigationAction.navigationType == UIWebViewNavigationTypeLinkClicked)
NSString *url = [navigationAction.request.URL query];
decisionHandler(WKNavigationActionPolicyAllow);
2) webViewDidStartLoad -> didStartProvisionalNavigation
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
3) webViewDidFinishLoad -> didFinishNavigation
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
4) didFailLoadWithError -> didFailNavigation
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
Migrando UIWebView a WKWebView, Rápido 4:
Equivalente de shouldStartLoadWithRequest
:
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
var action: WKNavigationActionPolicy?
defer
decisionHandler(action ?? .allow)
guard let url = navigationAction.request.url else return
print(url)
if navigationAction.navigationType == .linkActivated, url.absoluteString.hasPrefix("http://www.example.com/open-in-safari")
action = .cancel // Stop in WebView
UIApplication.shared.openURL(url) // Open in Safari
Equivalente de webViewDidStartLoad
:
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!)
print(String(describing: webView.url))
Equivalente de didFailLoadWithError
:
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error)
let nserror = error as NSError
if nserror.code != NSURLErrorCancelled
webView.loadHTMLString("404 - Page Not Found", baseURL: URL(string: "http://www.example.com/"))
Equivalente de webViewDidFinishLoad
:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
print(String(describing: webView.url))
Sección de Reseñas y Valoraciones
Si crees que te ha sido de ayuda este artículo, sería de mucha ayuda si lo compartes con el resto programadores de esta forma nos ayudas a difundir nuestra información.