0

Background: Using Xcode 8.3, swift 3, compiling for ios version 8

I have a very simple app that's just using webview to display my website. My website has external links(using hrefs) which I want to open in Safari when user clicks, not in my app's webview.

I found many similar questions with very similar answers. I tried to incorporate the answers I found, however, I cannot get mine to work.

Hoping to get some clarity.

Here is my code:

ViewController code

import UIKit

class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var siteView: UIWebView!
override func viewDidLoad() {
    super.viewDidLoad()

    siteView.delegate = self

    // Do any additional setup after loading the view, typically from a nib.

    let url = URL(string: "myurlgoeshere")

    siteView.loadRequest(URLRequest(url: url!))
}

AppDelegate code

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, 
    navigationType: UIWebViewNavigationType) -> Bool {

    if navigationType == UIWebViewNavigationType.linkClicked {

        guard let url = request.url else { return true }

        UIApplication.shared.openURL(url) //ios 8 version of open

        return false
    }
    return true

}

When I run the above code and click on link, they keep opening in my app.

I am running my code in the ios simulator, so not sure if that is keeping the links from opening in Safari.

Thank you in advance.

MaxAx
  • 101
  • 2
  • 12
  • Plz check Transport security policy if it has not secure website. – Dharma Jun 09 '17 at 11:51
  • I think I had to set this previously because my url is not https, however, the external links are. I wonder if there is a conflict now. – MaxAx Jun 09 '17 at 11:55
  • no worries Transport security only for our app not for safari. – Dharma Jun 09 '17 at 11:57
  • May be it will be helpful https://stackoverflow.com/questions/12416469/how-to-launch-safari-and-open-url-from-ios-app – Dharma Jun 09 '17 at 12:01

0 Answers0