繁体   English   中英

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

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

我正在更新iTunes应用程序,并且想利用新的SFSafariViewController api。 用例非常简单,当用户打开应用程序时,该应用程序会自动将设置的URL加载到SFSafariViewController中。

关于此的教程似乎很有限,一旦应用打开,确实存在的教程会通过链接涉及IBAction。

当用户单击其设备上的应用程序时,如何自动在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)

暂无
暂无

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

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