我应该如何确定 UIKit 将打开哪个场景,以便我可以在正确的 window 中设置我的 UI? (iOS 13+)
How should I determine which scene is going to be opened by UIKit so I can setup my UI in the proper window? (iOS 13+)
背景
我有一个支持多个 windows (iPadOS 13+) 的应用程序,我想知道响应用户点击通知的正确方式。我想根据用户点击的通知设置 UI。
我在 UNUserNotificationCenter
的共享实例上设置 UNUserNotificationCenterDelegate
属性,如下所示:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}
我遵从UNUserNotificationCenterDelegate
,实现了下面的方法。当用户与通知交互时,此方法总是触发:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// This gets called whenever a notification is tapped, dismissed or a custom action is performed by the user from the system Notification Center UI.
// Should my UI setup code live here?
completionHandler()
}
在 session 标题为 Targeting Content with Multiple Windows 中,Apple 解释说当您的应用支持多个 windows 时,系统将使用以下 属性 来帮助确定哪个 window 将在用户点击通知时显示给用户:
// An identifier for the content of the notification used by the system to customize the scene to be activated when tapping on a notification.
response.notification.request.content.targetContentIdentifier
最终,UIKit 没有约定将打开哪个场景 (window) 以响应用户点击通知,但它会尽最大努力打开场景 (我指定的 window) 与通知的 targetContentIdentifier
最匹配。
如果用户在我的应用程序终止时点击通知,那么当在我的 SceneDelegate
上调用以下函数时,我将能够访问有关 [=21] 内的通知点击的信息=]:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
connectionOptions.notificationResponse
// Should my UI setup code live here?
// I don't think so because `connectionOptions.notificationResponse` is sometimes nil and this method is only called once when the scene is being connected to a `UISceneSession`
}
如果我的应用程序没有终止,则不会调用此函数。
问题
- 我应该如何确定 UIKit 将打开哪个场景,以便我可以在正确的 window 中设置我的 UI?
看来UNNotificationResponse
有个叫targetScene
的属性,根据这个属性来决定要更新的场景:
https://developer.apple.com/documentation/usernotifications/unnotificationresponse/3255096-targetscene
背景
我有一个支持多个 windows (iPadOS 13+) 的应用程序,我想知道响应用户点击通知的正确方式。我想根据用户点击的通知设置 UI。
我在 UNUserNotificationCenter
的共享实例上设置 UNUserNotificationCenterDelegate
属性,如下所示:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}
我遵从UNUserNotificationCenterDelegate
,实现了下面的方法。当用户与通知交互时,此方法总是触发:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// This gets called whenever a notification is tapped, dismissed or a custom action is performed by the user from the system Notification Center UI.
// Should my UI setup code live here?
completionHandler()
}
在 session 标题为 Targeting Content with Multiple Windows 中,Apple 解释说当您的应用支持多个 windows 时,系统将使用以下 属性 来帮助确定哪个 window 将在用户点击通知时显示给用户:
// An identifier for the content of the notification used by the system to customize the scene to be activated when tapping on a notification.
response.notification.request.content.targetContentIdentifier
最终,UIKit 没有约定将打开哪个场景 (window) 以响应用户点击通知,但它会尽最大努力打开场景 (我指定的 window) 与通知的 targetContentIdentifier
最匹配。
如果用户在我的应用程序终止时点击通知,那么当在我的 SceneDelegate
上调用以下函数时,我将能够访问有关 [=21] 内的通知点击的信息=]:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
connectionOptions.notificationResponse
// Should my UI setup code live here?
// I don't think so because `connectionOptions.notificationResponse` is sometimes nil and this method is only called once when the scene is being connected to a `UISceneSession`
}
如果我的应用程序没有终止,则不会调用此函数。
问题
- 我应该如何确定 UIKit 将打开哪个场景,以便我可以在正确的 window 中设置我的 UI?
看来UNNotificationResponse
有个叫targetScene
的属性,根据这个属性来决定要更新的场景:
https://developer.apple.com/documentation/usernotifications/unnotificationresponse/3255096-targetscene