XCode 7 - Swift - 通过 SFSafariViewController 自动加载站点 [英] XCode 7 - Swift - Automatically load site via SFSafariViewController

查看:35
本文介绍了XCode 7 - Swift - 通过 SFSafariViewController 自动加载站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这方面的教程似乎很有限,而那些确实存在的教程在应用程序打开后通过链接涉及 IBAction.

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

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

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

以下代码只是白页:

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

推荐答案

您当前的代码正在尝试从 呈现current 视图控制器 (ViewController)SFSafariViewController,它甚至还没有被显示出来.

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

尝试交换这个:

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

为此:

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

这篇关于XCode 7 - Swift - 通过 SFSafariViewController 自动加载站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆