Get parameter from URL Scheme - Swift Xcode

Gerwin picture Gerwin · Mar 16, 2016 · Viewed 11.1k times · Source

I've made a URL scheme in the Info.plist to call my app, but the URL also parses a string of data which I need to read.

After looking around for a little while I've found - http://www.informit.com/articles/article.aspx?p=2301787&seqNum=2

with the following code

var scheme: String!
var path: String!

var query: String!

func application(app: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool {

  scheme = url.scheme
  path = url.path
  query = url.query

  return true
}

supposedly the function is called whenever I call the app through a URL, the function however is never called, and I can't imagine that I would have to put the function in the viewDidLoad, if I did put it in the viewDidLoad it would be called everytime the app started.

Am I wrong im presuming that - the function doesn't need to be called in the viewDidLoad or is there a better way to get data from a URL scheme using the Info.plist to create the scheme

Answer

Gerwin picture Gerwin · Mar 16, 2016
var scheme: String!
var path: String!

var query: String!

func application(app: UIApplication, openURL url: NSURL, options: [String: AnyObject]) -> Bool {

  scheme = url.scheme
  path = url.path
  query = url.query

  return true
}

need to be in AppDelegate.Swift you can then access it in the viewcontroller by doing -

var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate;

and you can then call a variable by doing -

var something = pathScheme.appDelegate;