如何在 swift (XCode 8) 中处理自定义 url 方案? [英] How to handle custom url schemes in swift (XCode 8)?

查看:32
本文介绍了如何在 swift (XCode 8) 中处理自定义 url 方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用此函数从自定义 url 接收参数:

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

但是,该函数从未被调用,我收到警告:实例方法 'application(:openURL:sourceApplication:annotation:)' 几乎匹配可选参数 'application(:open:sourceApplication:annotation:)' 协议'UIApplicationDelegate.

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.

基于 XCode 8 警告实例方法几乎匹配可选要求我尝试使用@objc但没有成功.如何构建一个在应用程序打开时调用的函数并从自定义 url 方案中给我参数?

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?

推荐答案

请参阅 UIApplicationDelegate 的文档.在 Swift 3 下,委托方法是:

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

不是

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

但无论如何都已弃用.您应该使用(使用 iOS 9.0 及更高版本):

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

这篇关于如何在 swift (XCode 8) 中处理自定义 url 方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆