简体   繁体   English

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

[英]XCode 7 - Swift - Automatically load site via SFSafariViewController

I'm in the process of updating our iTunes app, and would like to take advantage of the new SFSafariViewController api. 我正在更新iTunes应用程序,并且想利用新的SFSafariViewController api。 The use case is extremely simple, when user opens app, the app automatically loads the set url in a SFSafariViewController. 用例非常简单,当用户打开应用程序时,该应用程序会自动将设置的URL加载到SFSafariViewController中。

There seem to be limited tutorials on this, and the ones that do exist involve IBActions via links once the app is open. 关于此的教程似乎很有限,一旦应用打开,确实存在的教程会通过链接涉及IBAction。

How do I automatically open a link in SFSafariViewController when user clicks the app on the their device? 当用户单击其设备上的应用程序时,如何自动在SFSafariViewController中打开链接?

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

Your current code is trying to present the current view controller ( ViewController ) from the SFSafariViewController , which isn't even being displayed yet. 您当前的代码正在尝试从SFSafariViewController呈现当前的视图控制器( ViewController ),甚至尚未显示。

Try swapping this: 尝试交换这个:

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

For this: 为了这:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM