0

I'm in the process of updating our iTunes app, and would like to take advantage of the new SFSafariViewController api. The use case is extremely simple, when user opens app, the app automatically loads the set url in a SFSafariViewController.

There seem to be limited tutorials on this, and the ones that do exist involve IBActions via links once the app is open.

How do I automatically open a link in SFSafariViewController when user clicks the app on the their device?

The following code just white pages:

import UIKit
import SafariServices

class ViewController: UIViewController
{
    private var urlString:String = "https://example.com"

    override func viewDidLoad()
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!)

        svc.presentViewController(self, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

----Working Code----

import UIKit
import SafariServices

class ViewController: UIViewController
{
    private var urlString:String = "https://example.com"

    override func viewDidLoad()
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidAppear(animated: Bool) {

        super.viewDidAppear(animated)

        let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!)

        self.presentViewController(svc, animated: true, completion: nil)
    }
}
Mike Purcell
  • 19,847
  • 10
  • 52
  • 89

1 Answers1

2

Your current code is trying to present the current view controller (ViewController) from the SFSafariViewController, which isn't even being displayed yet.

Try swapping this:

svc.presentViewController(self, animated: true, completion: nil)

For this:

self.presentViewController(svc, animated: true, completion: nil)
Craig Otis
  • 31,257
  • 32
  • 136
  • 234
  • Getting the following warning: `Warning: Attempt to present on whose view is not in the window hierarchy!` – Mike Purcell Nov 19 '15 at 17:09
  • 3
    Instead of calling that in `viewDidLoad`, call it in `viewDidAppear:` – EmilioPelaez Nov 19 '15 at 17:11
  • Extra credit: is there a way to remove the 'done' link from the SFSafariViewController nav bar? – Mike Purcell Nov 19 '15 at 17:20
  • It sounds like you probably just want the `SFSafariViewController` to _be_ your root view controller, instead of your currently-mostly-useless `ViewController` that has no job other than to present a different view controller. You can try some wizardry to remove the done button by accessing the `navigationItem` of your `SFSafariViewController`, possibly by subclassing it, but you're probably better off trying it as a root view controller, or just embedding a `UIWebView`. – Craig Otis Nov 19 '15 at 18:10
  • Got it, I'll post up another question pertaining to that part. Thanks again for the help. – Mike Purcell Nov 19 '15 at 19:20
  • 1
    Post for removing navbar: http://stackoverflow.com/questions/33813129/xcode-7-swift-sfsafariviewcontroller-hide-navbar – Mike Purcell Nov 19 '15 at 19:52
  • @MikePurcell You should probably ask as a separate question w/ the code that you're using and a screenshot. – Craig Otis Nov 24 '15 at 14:50