How to determine the first application download on iphone?

I want someone to download my application for the first time, so that it starts with the settings view that I have, and every time you start with the main view.

I could not find a way to determine if this is running the first time the application is launched. any ideas?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch NSUserDefaults *padFactoids; int launchCount; padFactoids = [NSUserDefaults standardUserDefaults]; launchCount = [padFactoids integerForKey:@"launchCount" ] + 1; [padFactoids synchronize]; NSLog(@"this is the number: %i of times this app has been launched", launchCount); if ( launchCount == 1 ) { NSLog(@"this is the FIRST LAUNCH of the app"); // do stuff here as you wish bbb = [[Blue alloc]init]; [window addSubview:bbb.view]; } if ( launchCount >= 2 ) { NSLog(@"this is the SECOND launch of the damn app"); // do stuff here as you wish rrr = [[Red alloc]init]; [window addSubview:rrr.view]; } [window makeKeyAndVisible]; return YES; } 

here Red and Blue are subclasses of uiviewcontroller in both classes, only one difference is that in - (void) viewDidLoad {self.view.backgroundcolor = [UIColor redColor]; } in the case of the red class and in the blue class, which shows the background blue, but when I run the application, its displays only in blue do not show red, where I am mistaken, what am I doing when I launch the application. And when it shows red color .....

+3
source share
4 answers

Here's how to do it. You will be glad to know that it is incredibly simple. These are exactly clear lines of code.

Add this code anywhere. Perhaps just in your application: doneFinishLaunchingWithOptions: routine in the AppDelegate.m file. Or, wherever you make general settings for your application. (However, make sure it will only work once .)

 NSUserDefaults *padFactoids; int launchCount; padFactoids = [NSUserDefaults standardUserDefaults]; launchCount = [padFactoids integerForKey:@"launchCount" ] + 1; [padFactoids setInteger:launchCount forKey:@"launchCount"]; [padFactoids synchronize]; NSLog(@"number of times: %i this app has been launched", launchCount); if ( launchCount == 1 ) { NSLog(@"this is the FIRST LAUNCH of the app"); // do stuff here as you wish } if ( launchCount == 2 ) { NSLog(@"this is the SECOND launch of the damn app"); // do stuff here as you wish } // enjoy! 

Almost every application, except the simplest, does this. Hope it helps. For theory, theoretically, you don’t have to worry about a “synchronous” call, but we found a huge number of real user runs, which are probably more reliable if you enable it.

PS DO NOT use Booleans in settings. If you are a new programmer, it is best to understand the default values ​​and therefore never support. Stick to integers. (The first time they are not used, they are always "integer zero", so you have no problem.) Easy Peasy. Hope this helps.

+18
source

check the flag with NSUserdefaults, if it is missing, you show a preference view and set this flag after setting these parameters.

 BOOL hasBeenStarted = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasBeenStarted"]; if (!hasBeenStarted) { NSLog(@"Show panel"); [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasBeenStarted"]; } 
+1
source

it would be better if you create a local file when the application starts. And check if the file exists or not at every start.

In the case of NSUserDefault , this will be reinitialized every time if the application does not work in the background.

 - (void)checkApplicationFirstLaunch { if(![self getFileExistence:@"appinstall"]) { // this is fresh install of the app [self createFileFromString:@"install" filename:@"appinstall"]; } else { // app was already installed } } 

Here are the methods I used above

 - (BOOL) getFileExistence: (NSString *) filename { BOOL IsFileExists = NO; NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *documentsDir = [documentPaths objectAtIndex:0]; NSString *favsFilePath = [documentsDir stringByAppendingPathComponent:filename]; NSFileManager *fileManager = [NSFileManager defaultManager]; // Check if the database has already been created in the users filesystem if ([fileManager fileExistsAtPath:favsFilePath]) { IsFileExists = YES; } return IsFileExists; } - (void)createFileFromString:(NSString *)string filename:(NSString *)filename { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory NSLog(@"%@", documentsDirectory); NSError *error; BOOL succeed = [string writeToFile:[documentsDirectory stringByAppendingPathComponent:filename] atomically:YES encoding:NSUTF8StringEncoding error:&error]; if (!succeed){ // Handle error here NSLog(@"Did not save: Error: %@", error); } } 
0
source
 BOOL hasBeenStarted = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasBeenStarted"]; if (!hasBeenStarted) { NSLog(@"Show panel"); [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasBeenStarted"]; [[NSUserDefaults standardUserDefaults]synchronize];// save to NSUserDefaults } 
0
source

All Articles