簡體   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