1

I want to show the terms and conditions only the first time of my app launch.

Currently I am writing my code in didFinishLaunchingWithOptions.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Handle launching from a notification
 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
    NSLog(@"Already Run");
}
else
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];

FirstPage * termsView = [storyboard instantiateViewControllerWithIdentifier:@"FirstPage"];

    UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:termsView];
    self.window.rootViewController=navController;

    // This is the first launch ever
}
}

UPDATE::

What I want when my application launched first time in TERMSandCONDITIONSVC I have an Accept Button. When User Accepts the aggrement then only He allowed go further.

Problem With Above Code:

When i run my app first time TermsandCondition displayed to me.I press home button the app goes in Background.Again I open The app from launcher and TermsandCondtion showed.

But When I put my app in Background and By adouble tapping Home button remove it from background and run the app I am directly transfered to my home screen.

No terms and conditions.

How Can i recover from this.

Rahul
  • 5,594
  • 7
  • 38
  • 92
  • Application needs one view as Root view controller so you will need to set up rootview controller regardless of if else condition. This means you need to set RootView Controller in your if condition as well – Aadil Keshwani Apr 29 '15 at 12:15
  • @AadilKeshwani see my story board..I have already set. I want to only change it when my app runs first time. See my screenshot – Rahul Apr 29 '15 at 12:16
  • You can try following code: `FirstPage * termsView = [self.storyboard instantiateViewControllerWithIdentifier:@"termsView"]; UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:termsView]; self.window.rootViewController=navController;` Make sure to give `termsView` as storyboard id – Aadil Keshwani Apr 29 '15 at 12:29

3 Answers3

1

You are getting black screen because no viewcontroller is set as root view.

Do one thing Write your else part in IF part also and change your controller from terms page to second page.

Try to put below code in didFinishLaunch and check.

self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

UINavigationController *navController

 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
    NSLog(@"Already Run");
    SecondView *secondView = [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];
    navController=[[UINavigationController alloc]initWithRootViewController:secondView];

}
else
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
    [[NSUserDefaults standardUserDefaults] synchronize];

     TermsView *termsView = [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];
    navController=[[UINavigationController alloc]initWithRootViewController:termsView];

// This is the first launch ever
    }
}
self.window.rootViewController=navController;
 return YES;

UPDATE: just drag below lines to agree button's click event and remove this lines from didFinishlaunching.

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
Malav Soni
  • 2,739
  • 1
  • 23
  • 52
1

You are instantiating your termsView in a wrong manner. In your storyboard assign the FirstPage view controller a storyboardID of FirstPage.

And then in your didFinishLaunchingWithOptions do the following

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];

FirstPage * termsView = [storyboard instantiateViewControllerWithIdentifier:@"FirstPage"];

This will find your main storyboard and then instantiate termsView with the ViewController based on the storyboardID provided(i.e. FirstPage).

Apoorv
  • 13,470
  • 4
  • 27
  • 33
1

If you want to setup first time launch of an iOS app. Using this code the terms and conditions will appears or shown only the first time of app launch.

  if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
    {
        NSLog(@"Already Run");
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        FirstPage *termsView = [[FirstPage alloc]init];

        UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:termsView];
        self.window.rootViewController=navController;

        // This is the first launch ever
UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
//first goto storyboard and give storyboard id "TermsConditionVC" to TermsConditionVC 
        TermsConditionVC *firstVC = [story instantiateViewControllerWithIdentifier:@"TermsConditionVC"];
        self.window.rootViewController =firstVC;
    }

for full article and complete process to setup your first timer iOS app launch visit below link
http://findnerd.com/list/view/How-to-setup-first-time-launch-of-an-iOS-App/2770/

Jagandeep Singh
  • 364
  • 4
  • 8