我面临的问题,我无法解决的通知和设计。
当打开一个深链接时,UIApplication用来发送一个带有userInfo的UIApplication.didFinishLaunchingNotification,其中包含了带有已打开的URL的UIApplicationLaunchOptionsURLKey。
所以我们可以这样订阅:
NotificationCenter.default.addObserver(forName: UIApplication.didFinishLaunchingNotification, object: nil, queue: nil) { (notification) in
print(notification.userInfo) // prints UIApplicationLaunchOptionsURLKey: url_that_opened_the_app
}现在,在iOS 13之后没有发生这种情况:userInfo是nil。
所以问题是:当应用程序打开一个深度链接时,有什么方法可以从notification接收通知吗?
*想法:*我认为这是由于UISceneDelegate现在负责打开deeplinks这一事实,这一点得到了以下事实的证实:如果我们删除SceneDelegate,我们就可以拿回我们的userInfo。我试图找出是否有任何SceneDelegate通知向我们提供了这样的信息,但它们没有:didActivateNotification和willConnectNotification都没有提供任何信息。
发布于 2021-07-26 18:16:25
是的,您也可以在SceneDelegate中找到同样的信息。
这是我做的示例。
一旦您从深链接启动应用程序,您将在
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
}您可以获得如下URL:
if let context = connectionOptions.urlContexts.first as? UIOpenURLContext {
print(context.url.absoluteString)
}因此,您的功能如下所示:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
if let context = connectionOptions.urlContexts.first {
print(context.url.absoluteString)
//Here you can pass this URL to your notification and perform your logic.
}
}我测试的DeepLink是:
deeplinks://mycustomDeepLink
此外,我改变了计划为Wait for the Executable to Launch,以检查初始发射的东西,从深链接。

注意:调试器有时由于任何原因都不能工作。因此,在我的示例中,我在ViewController中添加了一个警报视图。
https://stackoverflow.com/questions/68397978
复制相似问题