How to setup first time launch of an iOS App

最后都变了- 提交于 2020-01-16 00:54:10

问题


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.


回答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];



回答2:


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).




回答3:


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/



来源:https://stackoverflow.com/questions/29943288/how-to-setup-first-time-launch-of-an-ios-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!