gpt4 book ai didi

iOS UILocalNotification 启动特定屏幕

转载 作者:行者123 更新时间:2023-11-28 15:42:42 25 4
gpt4 key购买 nike

当用户点击 UILocalNotification 时,我尝试在特定屏幕上启动我的应用程序。这是我在 AppDelegate 中的代码:

 // MARK: - Local Notification
func application(_ application: UIApplication, didReceive notification: UILocalNotification)
{
if (application.applicationState == UIApplicationState.active)
{
print("Active")
}
else if (application.applicationState == UIApplicationState.background)
{
print("Background")
}
else if (application.applicationState == UIApplicationState.inactive)
{
print("Inactive")
print(notification.userInfo as Any)
self.redirectToPage(userInfo: notification.userInfo as! [String : String])
}
}

func redirectToPage(userInfo: [String : String]!)
{
if userInfo != nil
{
if let pageType = userInfo["TYPE"]
{
if pageType == "Page1"
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
self.window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "promoVC")
}
}
}
}

当应用程序处于后台或非事件状态时,它工作正常,但当它被暂停时,点击 UILocalNotification 以默认屏幕启动应用程序。当我的应用程序处于暂停状态时,如何在特定屏幕上启动我的应用程序?

最佳答案

在 didFinishLaunching 中也做所有事情,因为这是暂停应用程序通知的地方

例如

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
//allow notifs
let types : UIUserNotificationType = [.alert]
let settings = UIUserNotificationSettings(types: types, categories: nil)
application.registerUserNotificationSettings(settings)

//forward notif, if any
if let note = launchOptions?[UIApplicationLaunchOptionsKey.localNotification] as? UILocalNotification {
handleNotification(note: note, fromLaunch: true)
}
}

func application(_ application: UIApplication, didReceive note: UILocalNotification) {
handleNotification(note: note, fromLaunch: false)
}

func handleNotification(note:UILocalNotification, launch:Bool) {
if (application.applicationState == UIApplicationState.active)
{
print("Active")
}
else if (application.applicationState == UIApplicationState.background)
{
print("Background")
}
else if (application.applicationState == UIApplicationState.inactive)
{
print("Inactive")
print(notification.userInfo as Any)
self.redirectToPage(userInfo: notification.userInfo as! [String : String])
}
else {
print("other ;)")
print(notification.userInfo as Any)
self.redirectToPage(userInfo: notification.userInfo as! [String : String])
}

}

关于iOS UILocalNotification 启动特定屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43536796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com