Swift - WKWebKit - cannot open links in external browser (Safari)
up vote
3
down vote
favorite
I am loading a content from a google drive document (published) in a WKWebView but I cannot get some of the links to open. I am trying to open them in Safari, but if I can get them to open inside the webview is ok also.
I am using the WKUIDelegate with this method:
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
let url = navigationAction.request.url!
print(navigationAction.request.url!)
if navigationAction.targetFrame == nil, UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(navigationAction.request.url!, options: [:], completionHandler: nil)
}
return nil
}
The issue here is that navigationAction.request.url! is "" (empty) for some links and so I cannot open them (canOpenURL returns false)
For example, the links I cannot open look like this:
<a class="c6" href="https://www.google.com/url?q=https://www.youtube.com/watch?v%3DYK.....">Tutorial</a>
And the links I can open:
<a target="_blank" title="Learn more about Google Drive" href="//docs.google.com/">Google Drive</a>
In the last case, the navigationAction.request.url is valid as written in the tag.
Is there a way to solve this?
EDIT:
I tried long press on a link and copy link and it seems that the URL is copied correctly, so there is no problem in the loaded html.
After another few tests I discovered that some issues are with the actual links, for example if I try to open https://google.com/url.... https://docs.google.com... or youtube.com or some other sites it works, but if I try to open https://www.google.com/... it does not work
ios swift uiwebview wkwebview
add a comment |
up vote
3
down vote
favorite
I am loading a content from a google drive document (published) in a WKWebView but I cannot get some of the links to open. I am trying to open them in Safari, but if I can get them to open inside the webview is ok also.
I am using the WKUIDelegate with this method:
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
let url = navigationAction.request.url!
print(navigationAction.request.url!)
if navigationAction.targetFrame == nil, UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(navigationAction.request.url!, options: [:], completionHandler: nil)
}
return nil
}
The issue here is that navigationAction.request.url! is "" (empty) for some links and so I cannot open them (canOpenURL returns false)
For example, the links I cannot open look like this:
<a class="c6" href="https://www.google.com/url?q=https://www.youtube.com/watch?v%3DYK.....">Tutorial</a>
And the links I can open:
<a target="_blank" title="Learn more about Google Drive" href="//docs.google.com/">Google Drive</a>
In the last case, the navigationAction.request.url is valid as written in the tag.
Is there a way to solve this?
EDIT:
I tried long press on a link and copy link and it seems that the URL is copied correctly, so there is no problem in the loaded html.
After another few tests I discovered that some issues are with the actual links, for example if I try to open https://google.com/url.... https://docs.google.com... or youtube.com or some other sites it works, but if I try to open https://www.google.com/... it does not work
ios swift uiwebview wkwebview
As a workaround, I just replaced all "/www.google.com/" expressions with "/google.com/". Seems to be working like this.
– Andrei F
Nov 11 at 14:14
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am loading a content from a google drive document (published) in a WKWebView but I cannot get some of the links to open. I am trying to open them in Safari, but if I can get them to open inside the webview is ok also.
I am using the WKUIDelegate with this method:
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
let url = navigationAction.request.url!
print(navigationAction.request.url!)
if navigationAction.targetFrame == nil, UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(navigationAction.request.url!, options: [:], completionHandler: nil)
}
return nil
}
The issue here is that navigationAction.request.url! is "" (empty) for some links and so I cannot open them (canOpenURL returns false)
For example, the links I cannot open look like this:
<a class="c6" href="https://www.google.com/url?q=https://www.youtube.com/watch?v%3DYK.....">Tutorial</a>
And the links I can open:
<a target="_blank" title="Learn more about Google Drive" href="//docs.google.com/">Google Drive</a>
In the last case, the navigationAction.request.url is valid as written in the tag.
Is there a way to solve this?
EDIT:
I tried long press on a link and copy link and it seems that the URL is copied correctly, so there is no problem in the loaded html.
After another few tests I discovered that some issues are with the actual links, for example if I try to open https://google.com/url.... https://docs.google.com... or youtube.com or some other sites it works, but if I try to open https://www.google.com/... it does not work
ios swift uiwebview wkwebview
I am loading a content from a google drive document (published) in a WKWebView but I cannot get some of the links to open. I am trying to open them in Safari, but if I can get them to open inside the webview is ok also.
I am using the WKUIDelegate with this method:
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
let url = navigationAction.request.url!
print(navigationAction.request.url!)
if navigationAction.targetFrame == nil, UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(navigationAction.request.url!, options: [:], completionHandler: nil)
}
return nil
}
The issue here is that navigationAction.request.url! is "" (empty) for some links and so I cannot open them (canOpenURL returns false)
For example, the links I cannot open look like this:
<a class="c6" href="https://www.google.com/url?q=https://www.youtube.com/watch?v%3DYK.....">Tutorial</a>
And the links I can open:
<a target="_blank" title="Learn more about Google Drive" href="//docs.google.com/">Google Drive</a>
In the last case, the navigationAction.request.url is valid as written in the tag.
Is there a way to solve this?
EDIT:
I tried long press on a link and copy link and it seems that the URL is copied correctly, so there is no problem in the loaded html.
After another few tests I discovered that some issues are with the actual links, for example if I try to open https://google.com/url.... https://docs.google.com... or youtube.com or some other sites it works, but if I try to open https://www.google.com/... it does not work
ios swift uiwebview wkwebview
ios swift uiwebview wkwebview
edited Nov 7 at 15:43
asked Nov 6 at 19:51
Andrei F
1,58851942
1,58851942
As a workaround, I just replaced all "/www.google.com/" expressions with "/google.com/". Seems to be working like this.
– Andrei F
Nov 11 at 14:14
add a comment |
As a workaround, I just replaced all "/www.google.com/" expressions with "/google.com/". Seems to be working like this.
– Andrei F
Nov 11 at 14:14
As a workaround, I just replaced all "/www.google.com/" expressions with "/google.com/". Seems to be working like this.
– Andrei F
Nov 11 at 14:14
As a workaround, I just replaced all "/www.google.com/" expressions with "/google.com/". Seems to be working like this.
– Andrei F
Nov 11 at 14:14
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53179021%2fswift-wkwebkit-cannot-open-links-in-external-browser-safari%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
As a workaround, I just replaced all "/www.google.com/" expressions with "/google.com/". Seems to be working like this.
– Andrei F
Nov 11 at 14:14