Gisela Kottmeier Gisela Kottmeier - 1 month ago 19
iOS Question

How to handle custom url schemes in swift (XCode 8)?

I tried using this function to receive the parameters from the custom url:

func application(_ application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return true;
}


However, the function is never called and I receive warning: "Instance method 'application(:openURL:sourceApplication:annotation:)' nearly matches optional argument 'application(:open:sourceApplication:annotation:)' of protocol 'UIApplicationDelegate.

Based on XCode 8 Warning "Instance method nearly matches optional requirement" I tried using @objc with no success. How can I build a function that will be called when the app is opened and give me the parameters from the custom url scheme?

Answer

See the documentation for UIApplicationDelegate. Under Swift 3 the delegate method is:

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    return true;
}

Not

func application(_ application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    return true;
}

But that one is deprecated anyway. You should be using (with iOS 9.0 and later):

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    return true;
}