How open a specific uitabbarcontroller from AppDelegate with push notifications? - swift

I'm using push notifications on my app. So, when I receive a notification and touch it, I want go to another UITabbarcontroller, but when I doing it the app only show the UINavigationbar and don't the UITabbarcontroller. This a screenshot -->
And this my code.
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "RequestDriverViewController") as! RequestDriverViewController
let nc = UINavigationController(rootViewController: vc)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = nc
self.window?.makeKeyAndVisible()

I was able to solve it:
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TabBarIdentifier") as! UITabBarController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()

Related

How to change the entry point of a Ios App?

I want to change my entry Point of my project but how I can do that? I don't use the storyboard, I do it all programmatically and I want my secondViewController to be the firstViewController.
Thanks in advance!
without storyboard start with your specific ViewController put this code in didfinishlaunchingwithOptions method in AppDelegate.
let homeVC = SecondViewController()
self.window?.rootViewController = homeVC
self.window?.makeKeyAndVisible()
with storyboard
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let homeVC = storyboard.instantiateInitialViewController()
self.window?.rootViewController = homeVC
self.window?.makeKeyAndVisible()

How can i return to Main.storyboard which contains a TabBarController from another Storyboard

I have two storyboards, one which contains everything the app really is, and another which contains an "onboarding"/tutorial to my app.
Once the tutorial is done, I want to navigate back to my original view controller.
This is my code to navigate to the other storyboard:
let defaults = UserDefaults.standard
if defaults.bool(forKey: "firstOpened") {
print("First VC launched")
}else{
var vc: UIViewController
let goTo = UIStoryboard(name: "Onboarding", bundle: nil).instantiateViewController(withIdentifier: "notificationStoryboard") as! FirstOnboardingViewController
self.present(goTo, animated: true, completion: nil)
}
With this, it works, except the TabBarController is not showing, and not working the way I want it to.
And this is my code to navigate back to the Main.Storyboard:
#objc func handleSecondPush() {
//registerForRemoteNotifications()
UserDefaults.standard.set(true, forKey: "firstOpened")
let goTo = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "pushToFeedVC")
self.present(goTo, animated: true, completion: nil)
//self.performSegue(withIdentifier: "goToLink", sender: nil)
}
I have also tried this in the other Storyboard, but with this the button doesn't change the view:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
print(controller)
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
if let tabBarVc = self.window?.rootViewController as? UITabBarController {
tabBarVc.selectedIndex = 1
}
Question in short terms: So my question is, how can I navigate back to the main.storyboard which will contain the TabBarController with a selected index of 1, from a storyboard that doesn't contain navigation controller or a TabBarController?
When you present the onbaording you should return back to the tab with with
self.dismiss(animated:true,completion:nil)
for complex presentations you can do this for easy re-back
let vc = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = vc
What is better is to
let goTo = UIStoryboard(name: "Onboarding", bundle: nil).instantiateViewController(withIdentifier: "notificationStoryboard") as! FirstOnboardingViewController
let nav = UINavigationController(rootViewController:goTo)
nav.isNavigationBarHidden = true
self.present(nav, animated: true, completion: nil)
the flow inside the onbarding should be with pushViewController
then to dismiss in the last onbaording vc
if let tab = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController as? UITabBarController {
tab.dismiss(animated:true,completion:nil)
}

Navigation Bar is shown only at the first tabBarController

I've got a tabBar with 3 screens (Home, Favorite and Settings). Use Storyboard. All these screens have a NavigationController. Navigation Bar is shown only at the first screen - Home, other have no Navigation Bar (no title, no button). Why is this happening?
Upd: I have this problem only on Simulator but on real iPhone everything is good.
My AppDelegate:
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.makeKeyAndVisible()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "TabBarController")
self.window?.rootViewController = initialViewController
Also I have Login page before TabBarController, going to TabBarController from Login by this method:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let singInViewController = storyboard.instantiateViewController(withIdentifier: "TabBarController")
UIApplication.shared.keyWindow?.rootViewController = singInViewController
self?.present(singInViewController, animated: true, completion: nil)
Add Each View Controller Separate Navigation Bar and attach UINavigation Controller to Tab Bar Controller.

How to find out a tab bar controller inside a SlideMenuController swift?

I have implemented an EXSlideMenuController and added a TabBarController as a main view controller in the sliding menu controller but now I want to find out the first view controller of TabBarController which is inside the EXSlideMenuController.
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let menuViewController = storyboard.instantiateViewController(withIdentifier: "menuViewController") as! MenuViewController
let initialViewController = storyboard.instantiateViewController(withIdentifier: "TabBarControllerID") as! TabBarController
let revealController = ExSlideMenuController(mainViewController: initialViewController, leftMenuViewController: menuViewController)
self.window?.rootViewController = revealController
self.window?.makeKeyAndVisible()
how do I find out the first view controller of tab bar controller from the ExSlideMenuController?
Here is the solution to find out the first view controller of a tab bar controller inside an ExSlideMenuController
let rootController = self.window?.rootViewController as! ExSlideMenuController
let customTabBarController = rootController.mainViewController as! CustomTabBarController
let navBarController = customTabBarController.viewControllers?.first as! UINavigationController
let storeController = navBarController.viewControllers.first as! StoreListViewController
may be it will help some other person.

Present ViewController from UNUserNotificationCenterDelegate

In my UNUserNotificationCenterDelegate method I want to jump the user to a specific view controller. I can easily get the VC itself from the storyboard:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "beach")
What I'm not entirely sure of though is now how to actually present it, since I don't have an object handy to call presentViewController on.
If your root is navigationVC
if let nav = self.window?.rootViewController as? UINavigationController {
nav.pushViewController(vc, animated: true)
}