iOS当应用关闭时,深层链接回调不起作用



我在我的应用程序中使用深度链接,并在我的SceneDelegate中使用此代码来segue到视图控制器。

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
for context in URLContexts {
print("url: (context.url.absoluteURL)")
print("scheme: (context.url.scheme ?? "")")
print("host: (context.url.host ?? "")")
print("path: (context.url.path)")
print("query: (context.url.query ?? "")")
print("components: (context.url.pathComponents)")
}
window?.rootViewController?.performSegue(withIdentifier: "splashToCreateNewPassword", sender: nil)
}

当应用程序已经在后台打开时,它工作得很好,但是当用户关闭应用程序时,它就不能工作了。它只是打开应用程序的第一个屏幕。

我也有同样的问题。在iOS(flutter build)中,我通过添加"可用内容"来解决这个问题。文章在这里:苹果内容可用文档。我使用onessignal,所以在api中我添加了该字段。现在,即使应用程序被强制关闭,它也会被唤醒,深度链接也会起作用。完整的onessignal邮差代码为:

{
"app_id": "1234",
"included_segments": ["Test"],
"content_available" : true,
"contents": {
"en": "Hi"
},
"data": {
"dynamic_link": "https://google.com"
},
"headings": {
"en": "Testing"
}
}

你可以从这个委托func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)获取URL

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let userActivity = connectionOptions.userActivities.first {
if let incomingURL = userActivity.webpageURL {
window?.rootViewController?.performSegue(withIdentifier: "splashToCreateNewPassword", sender: nil)
}
}
}

对于那些只使用AppDelegate的用户

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

if let userActDic = launchOptions?[UIApplication.LaunchOptionsKey.userActivityDictionary] as? [String: Any],
let userActivity  = userActDic["UIApplicationLaunchOptionsUserActivityKey"] as? NSUserActivity {
// Do with user activity
}
}

最新更新