Xcode 7 - Swift - Automatic site loading via SFSafariViewController

I am currently updating our iTunes application and want to use the new SFSafariViewController api. The use case is extremely simple, when the user opens the application, the application automatically loads the set URL into the SFSafariViewController.

This seems to be limited to tutorials, and those that exist are linked to IBActions via links when the application is open.

How to automatically open a link in SFSafariViewController when a user clicks an application on their device?

The following code contains only 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. } } 

---- Work 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) } } 
0
source share
1 answer

Your current code is trying to present the current view controller ( ViewController ) from an SFSafariViewController that is not yet displayed.

Try replacing this:

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

For this:

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

Source: https://habr.com/ru/post/1247097/


All Articles