title: Open specific link in Safari from UIWebView
body: <p>I have a <code>UIWebView</code> which loads a local index.html file.</p>
  
  <p>However I have an external link in this html file that I'd like to open in Safari instead of internally in the <code>UIWebView</code>.</p>
  
  <p>Opening a link in safari from say a <code>UIButton</code> was simple enough:</p>
  
  <pre><code>UIApplication.sharedApplication().openURL(NSURL(string: "http://www.stackoverflow.com"))
  </code></pre>
  
  <p>Opening the Instagram app from a external link also works like a charm.</p>
  
  <pre><code>&lt;a href="instagram://media?id=434784289393782000_15903882"&gt;instagram://media?id=434784289393782000_15903882&lt;/a&gt;
  </code></pre>
  
  <p>So my first though was to do something like this:</p>
  
  <pre><code>&lt;a href="safari://stackoverflow.com"&gt;Open in Safari&lt;/a&gt;
  </code></pre>
  
  <p>However that doesn't seem to work, then I read something about using <code>webView:shouldStartLoadWithRequest:navigationType:</code></p>
  
  <p>But everyone who's managed to open an external link in Safari is writing in Obj-C which I'm not too familiar with as I'm writing in Swift.</p>
  
  <p>Update with Swift Code:</p>
  
  <pre><code>import UIKit
  
  class AccessoriesViewController: UIViewController, UIWebViewDelegate {
  
  
      @IBOutlet weak var webView:UIWebView!
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
  
          if let url = NSBundle.mainBundle().URLForResource("accessories", withExtension: "html") {
              webView.loadRequest(NSURLRequest(URL: url))
          }
  
  
  
      }
      override func preferredStatusBarStyle() -&gt; UIStatusBarStyle {
          return UIStatusBarStyle.LightContent;
      }
  
      func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -&gt; Bool {
          if let url = request.URL where navigationType == UIWebViewNavigationType.LinkClicked {
              UIApplication.sharedApplication().openURL(url)
              return false
          }
          return true
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
  
  }
  </code></pre>