How to automatically navigate to a screen on start-up - ios

When developing an app, if I only want to test one screen, say, the third tab of a tab view, how would I make the app navigate there on start up?

I think a good solution is use a UI test to automatically navigate to the correct place in the app. Pausing on a breakpoint at the end of the test leaves the app to be played with manually.

rootViewController of window property in AppDelegate will be the first view controller shown on screen. you can make it by programming or using storyboard
by programming:
if NavigationController is the rootViewController of your window, put your own viewController in the first place (index at 0) of NavigationController's viewControllers which is an array. it will be on screen by default.
customNavigationController.viewControllers = [yourViewController]
or simply set your viewController to the rootViewController to the window property in appDelegate
AppDelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
// window?.rootViewController = CustomTabBarViewController()
// customViewController will show on screen by default.
window?.rootViewController = CustomViewController()
window?.makeKeyAndVisible()
return true
}
by interface builder:
check your viewControlelr's attributes inspector panel, and check "is Initial View Controller" option, then you can see a simple arrow attached to this view controller.
or:
add identifier to your viewController, and fetch it from storyBoard, then set it to the rootViewController of the window object in appDelegate
let testController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "testController") as! TestController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = testController

Related

How to set a UITabBarController as rootViewController from Storyboard

Short question:
How can I launch and make a UITabBarController be the rootViewController of my app after starting with a Storyboard?
Long question:
I'm not a swift expert, but I managed to create a complete app using XIBs from the beginning. Now I need my app to start with a Storyboard as a new requirement to post updates to the appstore from 01/07/2020, but I never used it to build my views. It was easy to modify my app to have my Storyboard as an entry point, but the problem is that my initial view today is a TabController, and I don't know how to navigate from my initial Storyboard to my TabController.
My AppDelegate today works something like this:
var window: UIWindow?
func application(_application: UIApplication, didFinishLauchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
// initiate UINavigationControllers and UITabBarController here...
tabController.viewController = [nav1, nav2, nav3, nav4]
tabController.modalPresentationStyle = .fullScreen
self.window!.rootViewController = tabController
self.window!.makeKeyAndVisible()
}
All my attempts ended with a white screen after showing my Storyboard without showing my TabBar.
One of these attempts was this:
override func loadView() {
super.loadView()
// initiate tabController the same way I did in the AppDelegate
UIApplication.shared.keyWindow?.rootViewController = tabController
}
Check the value "Is initial View Controller" for it.

Failed to instantiate view controller. - Swift

So, I'm trying to instantiate my view controller programmatically using storyboard references.
I've put this code in the AppDelegate:
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialController: WelcomePageViewController = mainStoryboard.instantiateViewController(withIdentifier: "WelcomePageViewController") as! UITabBarController
window?.rootViewController = initialController
window?.makeKeyAndVisible()
return true
}
And set this inside my view controller's storyboard:
However when I run the application, only a black screen is shown and this message:
"Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?"
I've used this exact code in other apps and it works just fine.
I've tried to clean build folder, to run it on an actual device and to create and instantiate a different storyboard file but nothing worked.
Open your Main.storyboard file and find WelcomePageViewController. When it’s selected, go to the attributes inspector and check the box marked Is Initial View Controller.
You should see a right-facing arrow appear to the left of WelcomePageViewController, showing that it’s your storyboard’s entry point.
Now you're all set to go!!!
Ok So i think here is something you can do if it suits:
set entry point to the navigation controller
class that navigation controller with a file like rootNavigationVC.swift which is subclass of UINavigationController
now in didLoad() you can set it's viewController or push one based on your condition
trust me its better than coding in AppDelegate.swift which never worked for me.

Present a UIViewController from an UINavigationController via App Delegate

I am building an iOS app in swift and upon launching the app I would like to first show viewController2, which is presented on top of viewController1, which is embedded in a UINavigationController. The key part being that ViewController2 is not part of the navigation stack and is presented instead of being pushed.
This is my current attempt which doesn't work and only shows ViewController1 upon launch.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController1 = mainStoryboard.instantiateViewController(withIdentifier: "VC1") as! ViewController1
let viewController2 = mainStoryboard.instantiateViewController(withIdentifier: "VC2") as! ViewController2
let navController = UINavigationController(rootViewController: viewController1)
viewController1.present(viewController2, animated: false)
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
return true
}
I have this structure in place because I enable a user to be able to swipe from left to right and right to left to get to viewController2 and viewController3 in a similar fashion to Snapchat. Presenting these controllers seemed the best idea as they don't have navigation bars and once finished you would want to return to viewControlller1 - perhaps I need to change the structure of my app but would ideally not like to as I aim to submit this mvp within the next week.
Help much appreciated from you iOS and swift wizards.
// Think I need to change the structure of the app to include viewController2 on the navigation stack :( as using accepted answer causes viewController1 to be seen briefly before viewController2 is presented which is logical - in the long run this will be better despite the short term pain
rootViewController should be set before everything. Do it in this order and it should work fine.
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
navController.present(viewController2, animated: false)

Access UIViewController from UITabBarController and UINavigationController

I am developing an application based on UITabbar and the view hierarchy as follows.
UITabBarController ----> UINavigationController ----> UIViewController
I have push notification payload which will open specific UIViewController, i can explicitly open UIViewController directly using view controller Storyboard ID, but tabBar and Navbar won't show.
How can I go to specific View Controller and show TabBar and NavController from AppDelegate didReceiveRemoteNotifications.
Thanks!
You have to instantiate all of your VC and set all of them as root of his predecessor :
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let yourVC = mainStoryboard.instantiateViewControllerWithIdentifier("YourVC_Identifier");
let yourNavController = mainStoryboard.instantiateViewControllerWithIdentifier("YourNAV_Identifier") as! UINavigationController
let yourTabController = mainStoryboard.instantiateViewControllerWithIdentifier("YourTAB_Identifier") as! UITabBarController
yourNavController.setViewControllers([yourVC], animated: false)
yourTabController.setViewControllers([yourNavController], animated: false)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = yourTabController
self.window?.makeKeyAndVisible()
return true
}
[[UIApplication sharedApplication] keyWindow] has a property .rootViewController. Presumably thats your tab bar. On this controller, you can set the active tab and switch out view controllers with the .viewControllers property. Now assuming one of these is your UINavigationController that should also have a property .rootViewController. Instantiate from the storyboard and either set the root or push the view controller on top of the navigation controller.
Follow the hierarchy from App Delegate programmatically.
In case your entry point is from the Storyboard, setup a UIWindow in your AppDelegate so you could set the UITabBarController as the following.
//self.tabBarController is you TabBar from Storyboard, or programatically initialized
self.window.rootViewController = self.tabBarController;
Then whenever you have a notification in didReceiveRemoteNotifications sort the notification out by type, and find the view controller:
//Let's say the View Controller being accessed is in the first position of the stack of viewcontroller from UITabBarController & UINavigationController
UINavigationController *navViewController = self.tabBarController.viewControllers.firstObject;
UIViewController *accessedViewController = navViewController.viewcontroller.firstObject;

Referencing TabbarController created from StoryBoard in AppDelegate?

I have a tabbar app with an initial login screen. The tabbarController is set as the initial view in Storyboard with 1 VC that has a navigationController also embed.
I have a loginVC instantiated and set as rootViewController in my AppDelegate. After the user has successfully sign in from the loginVC, I need to switch over to the tabbarController. Would I try to get a reference to the tabbarcontroller and set it as the new rootviewcontroller? If so, I'm having a hard time figuring out how to do this:/
AppDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if NSUserDefaults.standardUserDefaults().objectForKey("OAuth") == nil {
self.window = UIWindow(frame:UIScreen.mainScreen().bounds)
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var loginVC = storyboard.instantiateViewControllerWithIdentifier("LOGIN_VC") as LoginVC
self.window?.rootViewController = loginVC
self.window?.makeKeyAndVisible()
}
return true
}
This method gets called after user has successfully signed in
func dismissLoginVC() {
var tabbarController = self.storyboard?.instantiateViewControllerWithIdentifier("TABBAR") as UITabBarController
self.presentViewController(tabbarController, animated: true, completion: nil)
let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
appDelegate.window?.rootViewController = tabbarController
}
I know the problem with this is it just created a new tabbarcontroller, rather than referencing the existing tabbarController that was set as the initialView in storyboard. This seems to work but it is missing other items from the navigation bar.
Thanks for pointing me in the right direction!
I think you should change your app structure. Keep the tab bar controller as the initial view controller in the storyboard, and present modally (with no animation) the login controller from the viewDidAppear method of the controller in the first tab -- it will be the first thing the user sees. When you dismiss it, you will be back to that controller in the first tab. With this approach, you don't need any code in the app delegate.

Resources