XCode 7 – Swift – 通过SFSafariViewController自动加载站点

我正在更新我们的iTunes应用程序,并希望利用新的SFSafariViewController API。 用例非常简单,当用户打开应用程序时,应用程序会自动在SFSafariViewController中加载设置的url。

这似乎是有限的教程,而那些确实存在涉及通过链接IBActions一旦应用程序打开。

当用户点击设备上的应用程序时,如何在SFSafariViewController中自动打开链接?

以下代码只是白页:

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. } } 

—-工作代码—-

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

你当前的代码试图从SFSafariViewController中显示当前的视图控制器( ViewController ),它甚至还没有显示出来。

尝试交换这个:

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

为了这:

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