0

I have a very sneaky problem. I'm using Cordova to embeded my web-app onto iOS app.

But we have external URL and with our technique, it's really painfull to open external URL. And Cordova plugins like InAppBrowser does not work (because our app is already use an inapp browser).

Goal : we want to open an external URL, from a HTML page, to the Safari navigator of the device.

Maybe with Javascript ? I thought about something like :

UIApplication.shared.open(url, options: [:], completionHandler: nil)

So maybe, we can add in MainViewController a command triggered by an HTML button who use Javascript to open the URL in Safari ?

Anyone have an idea ? Many thanks

Gdebrig
  • 193
  • 1
  • 9

1 Answers1

0

check for canOpenURL then open

if let url = URL(string: "https://www.google.com"),
        UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.open(url, options: [:])
}
Mebin Joe
  • 2,172
  • 4
  • 16
  • 22
  • Thanks @Mebin, but it's pure swift code or JS ? (I've never touch Swift) – Gdebrig Apr 01 '19 at 10:06
  • @Gdebrig This is pure swift. Are you looking for JS ? – Mebin Joe Apr 01 '19 at 10:06
  • @Merbin yes or/and no, i'm looking for a solution to open an html url on the Safari app, maybe with JS – Gdebrig Apr 01 '19 at 10:08
  • @Gdebrig If you are using swift, the above should work fine. If you are looking for ios based webapp, please refer https://stackoverflow.com/questions/7930001/force-link-to-open-in-mobile-safari-from-a-web-app-with-javascript for JS solution. – Mebin Joe Apr 01 '19 at 10:10
  • it's working but not for us : we are in a hosted web-app context so we need to open the Safari App, this just opens the url in our web-app – Gdebrig Apr 01 '19 at 10:36
  • @Gdebrig Totally confused. what is the issue you facing and what yo need actually. could you please explain in detail. – Mebin Joe Apr 01 '19 at 10:54
  • yes sure, all explained here, in a Cordova-context : https://stackoverflow.com/questions/55304135/how-to-use-cordova-inappbrowser-into-a-hostedwebapps. We have a embeded web-app into a Cordova mobile app with some JS. But when we use external links, we have a issue. So we want to open Safari with the external URL and skirt our issue – Gdebrig Apr 01 '19 at 11:53
  • @Gdebrig So when you click the URL, need to open Safari App. Is that correct ? Javascript link action should redirect to some URL opened in Safari App. Is that what you are looking for ? – Mebin Joe Apr 01 '19 at 12:02
  • Yes exactly ! We have a link/button with an URL on the HTML, when you click on it, Safari oppens with the URL – Gdebrig Apr 01 '19 at 12:16
  • @Gdebrig are you using Cordova inappbrowser plugin ? – Mebin Joe Apr 01 '19 at 12:19
  • @Merbin, it's installed but not working. We have the same result than your first suggestion : the app open the external URL in the app and not with Safari (https://stackoverflow.com/questions/7930001/force-link-to-open-in-mobile-safari-from-a-web-app-with-javascript) – Gdebrig Apr 01 '19 at 12:21
  • @Gdebrig Instead of `_blank` try `_system`. Call the method `cordova.InAppBrowser.open(encodeURI('https://www.google.com/'), "_system");` "_system" will open url in Safari on iOS – Mebin Joe Apr 01 '19 at 12:25
  • Thanks for. We added your snip but same result... `lien` I think it's because our web-app is already using the plugin... – Gdebrig Apr 01 '19 at 12:47