When we are displaying a portal in web view, Sometimes it may fail to load the url.
This could be because of,
- No internet connection in the device
- Timeout
- Server is not reachable/responding
And there could be many other reasons.
Instead of displaying that Error in an alert, we can directly display that error description in the UIWebView in a HTML format.
Whenever UIWebView fails to load a page, UIWebViewDelegate method will get called.
-----------------------------------------------------------------------------------
- (void)webView:(UIWebView *)webView
didFailLoadWithError:(NSError *)error {
}
-----------------------------------------------------------------------------------
From this we can fetch the error and display the description using the loadHTMLString method of UIWebView.
-----------------------------------------------------------------------------------
- (void)webView:(UIWebView *)webView
didFailLoadWithError:(NSError *)error {
if (error) {
[self displayError: error];
}
}
- (void)displayError: (NSError*)error {
NSString *message = [NSString stringWithFormat:@"%@\n\n%@", error.localizedDescription, [error.userInfo objectForKey:@"NSErrorFailingURLStringKey"]];
NSString *htmlErrorDescription = [NSString
stringWithFormat:
@"%@", [UIFont mediumFontName], message];
[self.webView loadHTMLString:htmlErrorDescription
baseURL:nil];
}
-----------------------------------------------------------------------------------
Customize your own htmlErrorDescription by using respective HTML format we want to display the error.
The same logic could be used If you are using WKWebView from WebKit as UIWebView is deprecated now.
The same logic could be used If you are using WKWebView from WebKit as UIWebView is deprecated now.
Hope this post is useful. Feel free to comment incase of any queries.
No comments:
Post a Comment