In my Xcode project when a user taps on a notification I want to first send them to a certain item in my tabBar then I want to instantiate a view controller and send an object over to that view controller. I have code the that sends them to the tabBar I want, but I do not know how to instantiate them to the view controller while keeping the tabBar and navigation bar connected to the view controller. All the answers on this require you to change the root view controller and that makes me lose connection to my tabBar and navigation bar when the view controller is called.
A Real Life Example of this: User receives Instagram notification saying "John started following you" -> user taps on notification -> Instagram opens and shows notifications tab -> quickly send user to "John" profile and when the user presses the back button, it sends them back to the notification tab
Should know: The reason why I'm going to a certain tab first is to get that tab's navigation controller because the view controller I'm going to does not have one.
Here's my working code on sending the user to "notifications" tab (I added comments to act like the Instagram example for better understanding):
if let tabbarController = self.window!.rootViewController as? UITabBarController {
tabbarController.selectedViewController = tabbarController.viewControllers?[3] //goes to notifications tab
if type == "follow" { //someone started following current user
//send to user's profile and send the user's id so the app can find all the information of the user
}
}
In my last live project, I'm using the same approach like yours. So even though I doubt this method is the correct or ideal for handling a push notification from the AppDelegate (I still got a lot of stuff to learn in iOS
I can think of two ways to do that:
1) If that view controller is a UINavigationController you can simply push the profile from wherever you are:
2) Alternatively, what I like to do is control such things directly from my view controller subclass (in this case, PostListViewController). I have this helper method in a swift file that I include in all of my projects:
Then I would do this to push the new view controller:
First of all, you'll to insatiate a TabBarController:
And then insatiate all of the
viewControllers
of TabBarController. If your viewControllers is embedded in to theUINavigationController
? If so, you'll to insatiate a Navigation Controller instead:Also you should instantiate your desired ViewController too:
Make all of the NavigationControllers as
viewControllers
of TabBarController:And check: It's about your choice.
Make tabBarController as a
rootViewController
:Finally: It's your completed code:
If you want to present or push ViewController when the notification is tapped? Try something like that: