Reputation: 928
I am new to iOS development. I created a basic html page with hyperlink. I converted the html page into app using the following code.
class ViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var lupView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
lupView.loadRequest(URLRequest(url: Bundle.main.url(forResource: "lup_package/lup/index", withExtension: "html")!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Everything works good when I build it to iPad Air. But when I tap on the hyperlink, the target website is opening in the app itself. I would want that to be opened in a browser. Please help me to achieve that and it would be great if you explain me is that working.
Upvotes: 1
Views: 3145
Reputation: 3499
If you simply want to open a url using Safari, the following should help.
if let url = URL(string: "https://www.google.com") {
UIApplication.shared.open(url, options: [:])
}
In case you want to open hyperlinks on your WebView using Safari. Check this out. You need to use webView:shouldStartLoadWith
delegate method. Remember to set lupView.delegate = self
in your view controller or Storyboard.
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == .linkClicked {
// Open links in Safari
guard let url = request.url else { return true }
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
// openURL(_:) is deprecated in iOS 10+.
UIApplication.shared.openURL(url)
}
return false
}
// Handle other navigation types...
return true
}
Upvotes: 2