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










share|improve this question
























  • 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















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










share|improve this question
























  • 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













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini