AgnosticDev
AgnosticDev

Reputation: 1853

Objective-c Conditionally serve ViewControllers after app launches

I have a question about loading view controllers when an app launches. I want to have a condition that checks when the application launches based upon stored core data values and if true, it will load the view controller second in the stack. If false, I want to load the root view controller. I want to always preserve the root view controller not matter what the result of the condition, I just want to skip loading this view if the result of my condition is true and go right to the second view in the stack. I am not using storyboards. Has anyone done anything of this nature before?
Now, having said that, is this logic flow an acceptable solution to be implementing. Will there be issues during submission if I try something like this?

Upvotes: 0

Views: 256

Answers (3)

lancy
lancy

Reputation: 857

Check the conditions in the -applicationDidFinishLaunchingWithOptions: is the simple way.

However, we are suppose to keep the AppDelegate away from complicated. The best way to do the launching things, like check some condition, prepare the data, is to create a LaunchViewController as the rootViewController, you can make it the same looking as your app launch image. Then, you can presenting any VC you want with/without animation, it will be the first VC that user will see.

Let me know if you need more help.

Upvotes: 0

Caleb
Caleb

Reputation: 125017

Early in your app's launch, like in -applicationDidFinishLaunchingWithOptions:, check the condition in question and, if true, push the second controller onto the navigation stack. Specify NO for the animation parameter so that there's no obvious transition.

You can also set the nav controller's viewControllers property directly. Set it to an array with first and second controllers if the condition is true, or just the first controller otherwise.

Upvotes: 1

bgfriend0
bgfriend0

Reputation: 1152

Let's say you have the following view controllers:

UINavigationController *navigationController;
UIViewController *firstViewController;
UIViewController *secondViewController;

Then you can write code like this (edit: reworked solution based on comments below):

if (yourCondition)
    navigationController.viewControllers = @[ firstViewController, secondViewController ];
else
    navigationController.viewControllers = @[ secondViewController, firstViewController ];

Upvotes: 2

Related Questions