Albert K
Albert K

Reputation: 25

Elapsed time and setting a date?

Here is what I'm trying to accomplish: User touches a button, and and can share something to facebook. The date that the user touched the button is set, and for 24 seconds the user cannot share anything to facebook again. After 24 seconds, the user then is allowed to share another post to facebook.

The error is not facebook related, but it has something to do with the elapsedTime. After the elapsedTime is reset, it doesn't count back up to 24. It just stays at a very small number like "0.0043"

Here's my code:

-(void)postToFacebook
{
    if (elapsedTime >= 24) {
        [[CCDirector sharedDirector] pause];
        [[CCDirector sharedDirector] stopAnimation];

        CCAppDelegate *app = (CCAppDelegate*) [[UIApplication sharedApplication] delegate];


        SLComposeViewController *faceBookPost = [SLComposeViewController     composeViewControllerForServiceType:SLServiceTypeFacebook];

        [faceBookPost setInitialText:@"test"];
        [faceBookPost addURL:[NSURL URLWithString:@"http://www.google.com"]];

        [[app navController] presentModalViewController:faceBookPost animated:YES];
        faceBookPost.completionHandler = ^(SLComposeViewControllerResult result)
        {
            [[CCDirector sharedDirector] resume];
            [[CCDirector sharedDirector] startAnimation];
            [[app navController] dismissModalViewControllerAnimated:YES];
            dogeAmount.string = [NSString stringWithFormat:@"%.2LF", _doge];
            [self setDate];
            NSLog(@"Posted to Facebook");
        };
    } else if (elapsedTime < 24) {
        NSLog(@"%f", elapsedTime);
    }
}

And here is the SetDate that is called in the above:

-(void)setDate {
    NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date]
                                                          dateStyle:NSDateFormatterShortStyle
                                                          timeStyle:NSDateFormatterFullStyle];
    defaults = [NSUserDefaults standardUserDefaults];
    startTime = [NSDate date];
    calendar = [NSCalendar currentCalendar];
    components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:startTime];
    hour = [components hour];
    minute = [components minute];
    NSNumber *hourNumber = [NSNumber numberWithInteger:hour];
    NSNumber *minuteNumber = [NSNumber numberWithInteger:minute];
    [defaults setObject:startTime forKey:@"startTime"];
    [defaults setObject:hourNumber forKey:@"hour"];
    [defaults setObject:minuteNumber forKey:@"minute"];
    [defaults setInteger:elapsedTime forKey:@"elapsedTime"];
    NSLog(@"%@",dateString);
    elapsedTime = fabs([startTime timeIntervalSinceNow]);
    [defaults synchronize];
}

I can't figure out what I'm doing wrong here.

Upvotes: 0

Views: 142

Answers (5)

zaph
zaph

Reputation: 112857

Just save the date:

-(void)setStartTime {
    defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSDate date] forKey:@"startTime"];
}

Later retrieve the date:

- (BOOL)isTimeExpired {
    defaults = [NSUserDefaults standardUserDefaults];
    NSDate *startedDate = [defaults objectForKey:@"startTime"];
    NSTimeInterval elapsedTime = -[startedDate timeIntervalSinceNow];
    return elapsedTime > 24;
}

Note: NSDate simply is the number of seconds since the first instant of 1 January 2001, GMT.

Upvotes: 0

danh
danh

Reputation: 62676

The simple way to do this is to get a free timer from the performSelector:afterDelay: method on NSObject,,,

- (void)disablePosting {
    // set a property that disallows posting, like a bool, or even better
    // do something to the ui that disables, like disabling the button
    self.postingEnabled = NO;
}

- (void)enablePosting {
    self.postingEnabled = YES;
}

Then, to disable for 24 seconds, do this:

[self disablePosting];
[self performSelector:@selector(enablePosting) afterDelay:24];

And your FB code looks like this:

if (self.postingEnabled) {
    // do the fb request here, without any references to timeout other than

}

Now you can take all of the other date and elapsedTime related code out.

One little gotcha, remember to cancel the perform request on dealloc

- (void)dealloc {
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
}

Upvotes: 0

Paulw11
Paulw11

Reputation: 114875

Your fundamental problem is that you calculate elapsedTime in your setDate method, but you only call setDate once, immediately after you post Facebook, so elapsedTime is never going to change after that initial setting.

I presume that you are also storing the post date in NSUserDefaults so that you can keep track of the value across application launches? If so, where do you read the value back in? In viewDidLoad ?

I would get rid of the elapsedTime variable and create a postDate @property -

@property (strong,nonatomic) NSDate *postDate;

Then, in your viewDidLoad method -

self.postDate=[[NSUserdefaults standardUserDefaults] objectForKey:@"postDateKey"];

Now your postToFaceBook can be -

-(void)postToFacebook
{
    if (self.postDate ==  nil || [self.postDate timeIntervalSinceNow] > 24) {

        [[CCDirector sharedDirector] pause];
        [[CCDirector sharedDirector] stopAnimation];

        CCAppDelegate *app = (CCAppDelegate*) [[UIApplication sharedApplication] delegate];


        SLComposeViewController *faceBookPost = [SLComposeViewController     composeViewControllerForServiceType:SLServiceTypeFacebook];

        [faceBookPost setInitialText:@"test"];
        [faceBookPost addURL:[NSURL URLWithString:@"http://www.google.com"]];

        [[app navController] presentModalViewController:faceBookPost animated:YES];
        faceBookPost.completionHandler = ^(SLComposeViewControllerResult result)
        {
            [[CCDirector sharedDirector] resume];
            [[CCDirector sharedDirector] startAnimation];
            [[app navController] dismissModalViewControllerAnimated:YES];
            dogeAmount.string = [NSString stringWithFormat:@"%.2LF", _doge];
            self.postDate=[NSDate date];
            [[NSUserDefaults standardDefaults] setObject:self.postDate forKey:@"postDateKey"];
            NSLog(@"Posted to Facebook");
        };
    }
}

Upvotes: 0

Emil H&#246;rnlund
Emil H&#246;rnlund

Reputation: 48

.h

@property (strong, nonatomic) NSDate *dateString;

.m

- (void)share
{
    double dateSinceNow = -[self.dateString timeIntervalSinceNow];

    if (dateSinceNow > 24)
    {
        //share to facebook
    }

    self.dateString = [NSDate date];
}

Upvotes: 1

Andrew Monshizadeh
Andrew Monshizadeh

Reputation: 1784

You are constantly comparing [NSDate date] to now, which will only be the time between the instantiation and now. That is the reason it is always small.

Also, if you want to disable a button, why not disable it and schedule a method to enable it after 24 second delay?

Upvotes: 0

Related Questions