vanquish vanquish - 1 year ago 171
Swift Question

IOS Swift- App does not open specific selected tab bar controller index when push notification is received

My app has 5 tab bar and each time i receive push notification i would like the app to navigate to 3rd tab in the index. I able to achieve it when the app is in foreground or background (in active state). if the app is closed and i try to open the push notification the app opens and crashes.
Below is the code which i have

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
let tabbar:UITabBarController = self.window?.rootViewController as! UITabBarController
tabbar.selectedIndex = 3

Kindly let me know what i am doing wrong.

Answer Source

Try adding the below code in the end of diFinishLaunchingWithOptions method:

 if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String : AnyObject] {
            _ = notification["aps"] as! [String : AnyObject]

            (window?.rootViewController as! UITabBarController).selectedIndex = 3
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download