收到推送通知后重定向到特定屏幕 - Swift

时间:2017-02-01 17:21:17

标签: ios swift push-notification storyboard

我收到推送通知,并在有效负载上检查应用程序是否在后台。如果是,我想将用户重定向到消息屏幕,例如。我的应用程序有导航控制器和滑动菜单。我尝试使用此代码:

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
            if ( application.applicationState == UIApplicationState.inactive || application.applicationState == UIApplicationState.background  ) {
                     let storyboard = UIStoryboard(name: "Main", bundle: nil)
                     let vc = storyboard.instantiateViewController(withIdentifier: "Message")
                     window?.rootViewController = vc
            }
    }

然而,这个屏幕在崩溃时加载,因为它有许多组件丢失(UINavigation条等)。有没有其他方法可以重定向,也许在创建必要的segues时?

1 个答案:

答案 0 :(得分:0)

如果您缺少导航栏/导航项,您是否尝试将视图控制器推入导航堆栈,然后再将其设置为根vc?例如:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "Message")
let navVC = UINavigationController(rootViewController: vc)
window?.rootViewController = navVC