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

浪尽此生 提交于 2019-12-25 08:50:01

问题


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?


回答1:


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;
}


来源:https://stackoverflow.com/questions/40075122/how-to-handle-custom-url-schemes-in-swift-xcode-8

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!