![](/img/trans.png)
[英]How to open specific view controller when push notification received in swift 3
[英]IOS Swift- App does not open specific selected tab bar controller index when push notification is received
我的应用程序有5个选项卡栏,每次我收到推送通知时,我都希望该应用程序导航到索引中的第3个选项卡。 当应用程序处于前台或后台(处于活动状态)时,我能够实现它。 如果应用程序已关闭,而我尝试打开推送通知,则应用程序将打开并崩溃。 下面是我有的代码
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
{
let tabbar:UITabBarController = self.window?.rootViewController as! UITabBarController
tabbar.selectedIndex = 3
}
请让我知道我在做什么错。
尝试在diFinishLaunchingWithOptions
方法的末尾添加以下代码:
if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String : AnyObject] {
_ = notification["aps"] as! [String : AnyObject]
(window?.rootViewController as! UITabBarController).selectedIndex = 3
}
您要调试代码吗? 如果应用程序在那里崩溃,则很有可能是window.rootViewController不是UITabBarController。
您可以调试它,也可以更改as! 在吗? 和:
tabbar?.selectedIndex = 3
如果应用程序没有崩溃,则选项卡栏控制器在其他位置。 只要找到它。
还记得在选项字典中检查您的通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.