Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView catch HTTP error codes

Tags:

When i return any http error from my page (currently 401, but i tried also with 404 and so on)

http://min60.com/__personal/e401.php

the delegate callbacks of the WKWebView don't return an error

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error  - (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error { 

How to catch such errors?

like image 206
Peter Lapisu Avatar asked Oct 04 '15 17:10

Peter Lapisu


People also ask

What is WKWebView?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.

How do I use WKWebView?

Here's how: Open the XIB or Storyboard you want to add the web view to in Interface Builder. Find the web view or WKWebView in the Object Library at the bottom-left of Interface Builder. Drag-and-drop a WKWebView object from the Object Library to your view controller's canvas, and adjust its size and position.

How do I migrate to WKWebView?

You can implement WKWebView in Objective-C, here is simple example to initiate a WKWebView : WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init]; WKWebView *webView = [[WKWebView alloc] initWithFrame:self. view. frame configuration:theConfiguration]; webView.


1 Answers

The key was to wait for the response and then inspect the object, no error is called on http code

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {      if ([navigationResponse.response isKindOfClass:[NSHTTPURLResponse class]]) {          NSHTTPURLResponse * response = (NSHTTPURLResponse *)navigationResponse.response;         if (response.statusCode == 401) {              // here we go          }      }     decisionHandler(WKNavigationResponsePolicyAllow); } 
like image 150
Peter Lapisu Avatar answered Oct 12 '22 15:10

Peter Lapisu