Mustafa
Mustafa

Reputation: 775

Tap Counter App - Save Count on exit of App

I have a simple tap counter app, consisting of a button - which is pressed to count and a label which displays the count. I would like to save the count on exit of the screen/app, or if the home button is pressed or if the app crashes.

Here is my code so far:

h file:
int counter;

@interface tapcounter : UIViewController {

IBOutlet UILabel *count;
}
-(IBAction)click;

m file:
-(IBAction)click {
counter++;
count.text = [NSString stringWithFormat:@"%d",counter];

Any ideas on how I can best implement this? I am brand new to Xcode so will appreciate any help. Thanks.

Upvotes: 2

Views: 1164

Answers (5)

Satheesh
Satheesh

Reputation: 11276

You can use NSUserDefaults to store stuff permanently

NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
[userDefaults setObject:Any object you want forKey:any Key you want];
[userDefaults synchronize];

When application closes or crashes you can catch that by adding notifications:

Add this in your viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveCount) name:UIApplicationDidEnterBackgroundNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveCount) name:UIApplicationWillTerminateNotification object:nil];

Do the saving part in saveCount method

-(void)saveCount
{
 NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
 [userDefaults setObject:[NSString stringWithFormat:@"%d",counter] forKey:@"saveCount"];
 [userDefaults synchronize];
}

When you want to read the variable again use this - may be in ViewDidLoad

NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
NSString *countString= [userDefaults objectForKey:@"saveCount"];
NSLog(@"Your Count: %@",count);

//checking if data in user defaults is not empty
if(countString.length>0)
{
count.text=count;
counter=[countString intValue];
}

else
{
  //for first time 
  counter=0;
  count.text=@"0";
} 

assuming yourlocalCountVariable is incremented every time the button is pressed.

Upvotes: 2

joeByDesign
joeByDesign

Reputation: 133

You can use viewDidUnload and NSUserDefaults:

- (void)viewDidUnload
{
    [[NSUserDefaults standardUserDefaults] setObject:count forKey:@"tapCount"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

In addition to the above being in your view controller, you'll need some set up in your AppDelegate:

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

    // initialize defaults
    NSString *dateKey    = @"dateKey";
    NSDate *lastRead    = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:dateKey];
    if (lastRead == nil)     // App first run: set up user defaults.
    {
        NSDictionary *appDefaults  = [NSDictionary dictionaryWithObjectsAndKeys:[NSDate date], dateKey, nil];

        [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

    [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:dateKey];

    tapcounter *tapViewController = [[tapcounter alloc] init]; 

    if ([window respondsToSelector:@selector(setRootViewController:)]) {
        window.rootViewController = tapViewController;
    } else {
        [window addSubview:tapViewController.view];
    }

    [window addSubview:tapViewController.view];
    [window makeKeyAndVisible];

    return YES;
}

Upvotes: 1

nycynik
nycynik

Reputation: 7541

Use iCloud ;) It's overkill, but its great for learning.

Here is a tutorial. http://www.raywenderlich.com/6015/beginning-icloud-in-ios-5-tutorial-part-1

Upvotes: 1

Moray
Moray

Reputation: 321

Just use the NSUserDefaults. Here is how to implement it :

[[NSUserDefaults standardUserDefaults] setObject:counter forKey:@"count"] //set the value

[[NSUserDefaults standardUserDefaults] objectForKey:@"count"] //get the value

More information about NSUserDefaults with a tutorial here.

Upvotes: 1

woz
woz

Reputation: 11004

By far, the easiest way would be NSUserDefaults. Try this to save:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:count forKey:@"count"];
[defaults synchronize];

To retrieve it, use this:

if (![[NSUserDefaults standardUserDefaults] valueForKey:@"count"]) {
    NSInteger count = [NSUserDefaults integerForKey:@"count"];
    //use the value here
}

Upvotes: 1

Related Questions