Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a different entry point on first app launch? [duplicate]

Tags:

ios

storyboard

I have a Main.storyboard containing the root view controller of my app. However, I would like to display an onboarding view (without a navigation controller) on first app launch in order for the user to fill some data that's needed only once. So I was thinking about creating an Onboarding.storyboard, but I don't know how to choose which storyboard to set, where to do it (AppDelegate?) and when data is filled, how to change the current storyboard.

Thanks for your help.

like image 968
Rob Avatar asked Jun 05 '17 07:06

Rob


2 Answers

Try this for Swift 3.x, make sure you're setting Initial View Controller in both the storyboards. In your AppDelegate.swift file, application:didFinishLaunchingWithOptions

var storyboardName:String?
if someCondition {
    storyboardName = "Onboarding"
}else{
    storyboardName = "Main"
}

let storyboard = UIStoryboard(name: storyboardName, bundle: nil)

let intialVC = storyboard.instantiateInitialViewController()

self.window?.rootViewController = intialVC
like image 97
Imad Ali Avatar answered Oct 03 '22 07:10

Imad Ali


You need to change this in AppDelegate's didFinishLaunchingWithOptions method.This method call's during app launch every time.

How:

Consider this example. Store the values in NSUserdefaults and retrieve it during app launch.

Example

Objective C:

BOOL isCompletedSetupWizard =[[NSUserDefaults standardUserDefaults] boolForKey:@"isCompletedSetupWizard"];

if (isCompletedSetupWizard) 
{ 
   //Your code goes here
   //....
   //....
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    viewController = [storyboard instantiateViewControllerWithIdentifier:@"yourFirstView"];
    self.window.rootViewController = viewController;
}
else  
{
  //Your code goes here for another view controller.
  //....
  //....
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"onBoarding" bundle:nil];
    viewController = [storyboard instantiateViewControllerWithIdentifier:@"yourSecondView"];
    self.window.rootViewController = viewController;
}
like image 30
elk_cloner Avatar answered Oct 03 '22 07:10

elk_cloner