Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-23-2011, 02:07 AM   #1 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 9
letmusicring is on a distinguished road
Default Problem with BOOL and NSUserDefaults

I am trying to get my app to determine, upon launch, if the timer was running when the app quit (or went inactive). Here is my code:

View.m (defaults is [NSUserDefaults standardUserDefaults] and timerIsRunning is declared as "bool timerIsRunning" in the .h)
Code:
- (IBAction)clockIn:(id)sender {
	if (timerIsRunning == NO) {
        mainInt = [defaults floatForKey:@"wageIsPause"];
        randomMain = [NSTimer scheduledTimerWithTimeInterval:(0.1/1.0) target:self selector:@selector(randomMainVoid) userInfo:nil repeats:YES];
        timerIsRunning = YES;
        [defaults setBool:timerIsRunning forKey:@"timerRun"];
        [defaults synchronize];
In my delegate.m file
Code:
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSUserDefaults *defaultsOpen = [NSUserDefaults standardUserDefaults];
    [defaultsOpen synchronize];
    bool timerIsRunning = [defaultsOpen boolForKey:@"timerRun"];
    
    if (timerIsRunning == NO) {
        NSLog(@"Timer is not running.");
    }
    else {
        NSLog(@"Timer is running.");
    }
}
The problem is that when I open my app and the timer is running, it logs "Timer is not running." It also logs "Timer is not running." when the timer isn't running (which was to be expected.) I am getting no errors or warnings.

What am I missing here?

Last edited by letmusicring; 04-23-2011 at 02:08 AM. Reason: forgot part of code.
letmusicring is offline   Reply With Quote
Old 04-23-2011, 02:24 AM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

1. I don't see 'defaults' declared in 'clockIn:' method
2. What's the point of this calculation: 0.1/1.0 ?
3. You don't need to synchronize defaults before reading.
4. When you open the app, it will not log wether the timer is actually running or not but what is stored in user defaults. You need to restart the timer yourself.
baja_yu is offline   Reply With Quote
Old 04-23-2011, 02:37 AM   #3 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 9
letmusicring is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
1. I don't see 'defaults' declared in 'clockIn:' method
2. What's the point of this calculation: 0.1/1.0 ?
3. You don't need to synchronize defaults before reading.
4. When you open the app, it will not log wether the timer is actually running or not but what is stored in user defaults. You need to restart the timer yourself.
1. defaults is declared in view.h as [NSUserDefaults standardUserDefaults];
2. it's makes the timer run 10x as needed by the app.
3. Good to know, thanks.
4. The bool is set to YES in the 'clockIn:' method and saved to user defaults. Therefore, shouldn't the delegate be able to read the bool from user defaults and tell me whether it is set to YES or NO? It says NO even after it was set to YES.
letmusicring is offline   Reply With Quote
Old 04-23-2011, 03:47 AM   #4 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

You mean you made it as a macro in the header file? If it's not properly set then that's why nothing is being recorded if 'defaults' is nil. Try putting this in your IBAction and check the output

NSLog(@"defaults = %@", defaults);
baja_yu is offline   Reply With Quote
Old 04-25-2011, 12:33 AM   #5 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 9
letmusicring is on a distinguished road
Default

That was my problem, got it working. Thanks!
letmusicring is offline   Reply With Quote
Reply

Bookmarks

Tags
bool, iphone, nsuserdefaults

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 343
12 members and 331 guests
dansparrow, iOS.Lover, lorrettaui53, Nobbsy, Objective Zero, oztemel, pbart, PlutoPrime, samdanielblr, sledzeppelin, thephotographer, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:50 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0