iOS, Swift 5、AppDelegate open url / .onOpenUrl - 如何找到调用我的自定义方案的调用应用
iOS, Swift 5, AppDelegate open url / .onOpenUrl - how to find calling app that calls my custom scheme
所以在 UIKit 中处理 openURL 调用的旧方法是在 AppDelegate 中使用以下内容:
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { ...
不幸的是,新的@main SwiftUI 被重定向到
.onOpenUrl { url in ...
因此我们错过了字典 OpenURLOptionsKey.sourceApplication,我想确定哪个应用程序调用我的应用程序,然后可以执行适当的操作,因为我有一个用例需要限制某些方案 url 以供第 3 方应用而非其他应用。
执行此操作的最佳做法是什么?
我没有找到文档,也没有在这里搜索。看起来我可能已经通过 onContinueUserActivity/NSUserActivityTypeBrowsingWeb 获得了领先,但也没有被调用。
所以我设法在应用程序委托中使用了它:
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
let sceneConfig = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
sceneConfig.delegateClass = SceneDelegate.self
return sceneConfig
}
在 scenedelegate 中设置:
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
// get url and sourceApp out of first URLContext here
}
不幸的是,如果 URL 的发件人不是您的团队成员,您将在 UIOpenURLContext.sourceApp
中得到 nil
因此,如果您想在团队应用之间创建 RPC,那没问题。
如果您想在不同团队的应用程序之间进行通信,则必须想出一些其他方法来表明它是哪个应用程序,然后将您的响应策略应用于该应用程序。
所以在 UIKit 中处理 openURL 调用的旧方法是在 AppDelegate 中使用以下内容:
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { ...
不幸的是,新的@main SwiftUI 被重定向到
.onOpenUrl { url in ...
因此我们错过了字典 OpenURLOptionsKey.sourceApplication,我想确定哪个应用程序调用我的应用程序,然后可以执行适当的操作,因为我有一个用例需要限制某些方案 url 以供第 3 方应用而非其他应用。
执行此操作的最佳做法是什么? 我没有找到文档,也没有在这里搜索。看起来我可能已经通过 onContinueUserActivity/NSUserActivityTypeBrowsingWeb 获得了领先,但也没有被调用。
所以我设法在应用程序委托中使用了它:
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
let sceneConfig = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
sceneConfig.delegateClass = SceneDelegate.self
return sceneConfig
}
在 scenedelegate 中设置:
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
// get url and sourceApp out of first URLContext here
}
不幸的是,如果 URL 的发件人不是您的团队成员,您将在 UIOpenURLContext.sourceApp
中得到 nil因此,如果您想在团队应用之间创建 RPC,那没问题。 如果您想在不同团队的应用程序之间进行通信,则必须想出一些其他方法来表明它是哪个应用程序,然后将您的响应策略应用于该应用程序。