0

I made an iOS app with Xcode an Swift. All hyperlinks are opened in the apps own WebView. But I want to be able to open specific hyperlinks external, means in Safari browser.

I want to open all hyperlinks containing target=external as get parameter like this www.example.com/?target=external in Safari browser.

Cause I'm not very experienced I don't know how to start.

Can anybody give me a hint, a sample or a link? Thanks!

  • Have a look at the webview delegate methods, [WKWebView](https://developer.apple.com/reference/webkit/wknavigationdelegate) or [UIwebView](https://developer.apple.com/reference/uikit/uiwebviewdelegate) – rckoenes Feb 07 '17 at 20:51

1 Answers1

0
You can use below code to check 
if (url.query == target=external)
{// open in safari
UIApplication.sharedApplication().openURL(url)
}else{
//open in web view in app
}

// Note if you are exepecting more than 1 parameter in url you can use componentSeperatedbyString method 

var paramArry = url.query.componentsSeparatedByString("&")
if (paramArry[0] == target=external)
{// open in safari
UIApplication.sharedApplication().openURL(url)
}else{
//open in web view in app
}
Jasmeet Kaur
  • 1,538
  • 14
  • 16
  • Hi, thanks for your answer. Isn't is easier to use the following if more than one parameter? Code: `if url.query!.rangeOfString("target=external") != nil {}` –  Feb 07 '17 at 21:28
  • yes you can use it as well, it you don't need other parameters. – Jasmeet Kaur Feb 07 '17 at 21:29
  • Okay, which later code do you mean? Mine or yours? –  Feb 07 '17 at 21:31
  • You can use if url.query?.rangeOfString("target=external") != nil {} if you use "!" it will crash if there is no query parameter, so use "?" – Jasmeet Kaur Feb 07 '17 at 21:31
  • Okay, you're right `!` really crashed. I changed it to `?`. –  Feb 07 '17 at 21:40
  • Unfortunately I need more help. I currently have the code: https://codeshare.io/adwyMD. But that only have affect on the first URL, the main URL, but I should (only) work for the hyperlinks in this WebView. What to do? –  Feb 07 '17 at 21:43
  • Do you mean to open the hyperlinks from the web view? if so, do you want to open it to safari? – Jasmeet Kaur Feb 07 '17 at 21:47
  • I mean: I'm loading a URL in the WebView in my app. Almost all hyperlinks should get opened in the WebView. But hyperlinks containing `target=external` should get opened in Safari. Do you understand what I mean? –  Feb 07 '17 at 21:52
  • Yes I got it from where you are getting those url from server or they are present in webview? – Jasmeet Kaur Feb 07 '17 at 21:56
  • The URL's are in an HTML file that gets loaded from my server. It's like you have `www.example.com` as your main URL that gets loaded on first start and then click the `More information...` hyperlink. Imagine the `More information...` hyperlink is: `www.example.com/more_information.php?target=external`. –  Feb 07 '17 at 21:58