Home

Using SFSafariViewController

To display web contents in the app, it sounds like SFSafariViewController is useful, but I have never used it. So today, I'll write some example codes.

Create project

  1. Create sigle view application and open storyboard.
  2. Editor -> Embed in... -> Navigation Controller
  3. Add 3 buttons and connect to action.

Write examples

Add 3 methods to view controller.

Push

@IBAction func push(sender: AnyObject) {
    navigationController?.pushViewController(createSafariController(), animated: true)
}

Present modaly

@IBAction func present(sender: AnyObject) {
    let safariController = createSafariController()
    safariController.delegate = self
    presentViewController(safariController, animated: true, completion: nil)
}

Show with custom right button item

@IBAction func showWithRightButtonItem(sender: AnyObject) {
    let safariController = createSafariController()
    
    let barButtonItem = UIBarButtonItem(barButtonSystemItem: .Action, target: self, action: "barButtonItemDidTap:")
    safariController.navigationItem.rightBarButtonItem = barButtonItem
    
    navigationController?.pushViewController(safariController, animated: true)
}

Then run project...It's nice!

That's all. Sure enough, safari view controller is very convenient. I'll use this in the next app.

Source code

References