繁体   English   中英

iOS 13+:打开深层链接时无法获得通知 userInfo

[英]iOS 13+: Can't get a notification userInfo when opening a deeplink

我遇到了通知和深层链接无法解决的问题。

打开深层链接时 UIApplication 用于发送UIApplication.didFinishLaunchingNotificationuserInfo ,其中包含UIApplicationLaunchOptionsURLKey和打开的 URL。

所以我们可以像这样订阅它:

NotificationCenter.default.addObserver(forName: UIApplication.didFinishLaunchingNotification, object: nil, queue: nil) { (notification) in
    print(notification.userInfo) // prints UIApplicationLaunchOptionsURLKey: url_that_opened_the_app
}

现在,在 iOS 13 之后它不会发生: userInfonil

所以问题是:当应用程序打开深层链接时,有没有办法从通知中心接收通知?

*想法:* 我认为是UISceneDelegate现在负责打开deeplinks造成的,如果我们去掉SceneDelegate,我们就可以取回我们的userInfo,这也证实了这一点。 我试图找出是否有任何 SceneDelegate 通知为我们提供了此类信息,但它们没有: didActivateNotificationwillConnectNotification都没有给出任何信息。

是的,您也可以在SceneDelegate找到相同的SceneDelegate
这是我制作的样品

从深层链接启动应用程序后,您将收到connectionOptions

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
}

您可以按如下方式获取您的网址:

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添加了一个警报视图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM