Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS UILocalNotification启动特定屏幕_Ios_Swift_Uilocalnotification - Fatal编程技术网

iOS UILocalNotification启动特定屏幕

iOS UILocalNotification启动特定屏幕,ios,swift,uilocalnotification,Ios,Swift,Uilocalnotification,当用户点击UILocalNotification时,我正试图以特定屏幕启动我的应用程序。这是我在AppDelegate中的代码: // MARK: - Local Notification func application(_ application: UIApplication, didReceive notification: UILocalNotification) { if (application.applicationStat

当用户点击
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中完成整个过程,因为挂起应用程序的通知就是在这里发出的

e、 g


当应用程序挂起时,通知将被
didfishlaunchwithoptions
方法捕获。
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])
        }

    }