So I was in the process of converting my UIWebView app over to WKWebView only to find out that it won't open external websites, i.e. dropbox, facebook etc.
It loads my site in the viewDidLoad, so that's not an issue.
example:
NSURL *nsurl=[NSURL URLWithString:@"example.com"];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
webView.navigationDelegate = self;
webView.UIDelegate = self;
[self.view addSubview:webView];
I have called:
-(void)webView:(WKWebView *)webView didStartProvisionalNavigation: (WKNavigation *)navigation {}
- (void)webView:(WKWebView *)webView didFinishNavigation: (WKNavigation *)navigation{}
along with:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {}
decisionHandler(WKNavigationActionPolicyAllow);
and finally in my info.plist I added:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
Yet nothing works. What am I doing wrong?
Any help would as always be greatly appreciated.
The culprit is HTML attribute target set to value _blank which is used to signal links that should be opened in new tab instead of the current one. This target setting is meant to tell the browser (which is WKWebView in this case) that this link should be open in new tab by default. Which is the root of the problem.
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.
To clear old contents of webview With UIWebView you would use UIWebViewDelegate 's - webViewDidFinishLoad: .
Ok So I managed to get it, along with some help from some forgotten sites (sorry) I managed to get it all working.
So below is pretty much everything I'm using in my webview.
//Webview Start/Finish Request
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSLog(@"didStartProvisionalNavigation: %@", navigation);
}
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
NSLog(@"didReceiveServerRedirectForProvisionalNavigation: %@", navigation);
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
NSLog(@"didFailProvisionalNavigation: %@navigation, error: %@", navigation, error);
}
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
NSLog(@"didCommitNavigation: %@", navigation);
}
- (void)webView:(WKWebView *)webView didFinishLoadingNavigation:(WKNavigation *)navigation {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(@"didFinishLoadingNavigation: %@", navigation);
}
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
NSLog(@"didFailNavigation: %@, error %@", navigation, error);
}
- (void)_webViewWebProcessDidCrash:(WKWebView *)webView {
NSLog(@"WebContent process crashed; reloading");
}
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
if (!navigationAction.targetFrame.isMainFrame) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[self.webView loadRequest:navigationAction.request];
}
return nil;
}
- (void)webView:(WKWebView *)webView didFinishNavigation: (WKNavigation *)navigation{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(@"didFinish: %@; stillLoading:%@", [self.webView URL], (self.webView.loading?@"NO":@"YES"));
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
NSLog(@"decidePolicyForNavigationResponse");
decisionHandler(WKNavigationResponsePolicyAllow);
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
if (decisionHandler) {
decisionHandler(WKNavigationActionPolicyAllow);
}
}
So I help all of this helps somebody.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With