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.