views:

45

answers:

1

I am building an iphone app and would like to keep track of how many times it has been opened so that I can prompt the user to do certain actions after it has been opened X number of times. I have thought about storing a variable in Core Data which I update every time it is opened, but this seems like a waste since it is a singleton data, not multiple instances of an object. What is the best way to store data like this and access it without slowing down the app opening time?

+1  A: 

You can store it user defaults. Here is the code: Please apply some condition ...

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

..... [self countOpen];

}

-(void) countOpen

{

int _coutn= 0; _count= [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppCount"] intValue];

[[NSUserDefaults standardUserDefaults] setObject:[NSString stringFormat:@"%d",_count+1] forKey:@"AppCount"];

}

iPhoneDev
This looks like a decent solution - how can I auto-populate this with 0 to start with? Or does it simply return nil if there is no item in the standardUserDefaults object, and then I can set it to 1?
Jason
Yes first time it always come with NIL, then u can set to 1 and then start incrementing it.
iPhoneDev