How to handle custom url schemes in swift (XCode 8

2019-09-04 03:17发布

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?

标签: ios swift swift3
1条回答
等我变得足够好
2楼-- · 2019-09-04 04:05

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;
}
查看更多
登录 后发表回答