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) } } 
0

1 Answer 1

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) 
Sign up to request clarification or add additional context in comments.

7 Comments

Getting the following warning: Warning: Attempt to present <SFSafariViewController: 0x7fc362c0fd90> on <mothership.ViewController: 0x7fc3649ba4c0> whose view is not in the window hierarchy!
Instead of calling that in viewDidLoad, call it in viewDidAppear:
Extra credit: is there a way to remove the 'done' link from the SFSafariViewController nav bar?
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.