views:

47

answers:

1

Hey guys, So I am new to the development thing, making me yes a noob. I am currently creating my first iPhone app and in it I have a countdown relating to when a web video service is launching. Under the countdown I have a button that allows playing the video stream, I would like to create an if statement that tells the app to reference the iPhone current date and if it is over the launch date then the iPhone will launch the video stream, if the user presses the button before the launch date I would like a UIAlert View to appear saying the date of the service launch, so far I have this.

- (IBAction)watchLive { 

    self.today = [NSDate date];
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"MM/dd/yyyy hh:mm:ss"];
    NSString *launchDay = [[NSString alloc] initWithString:@"11/26/2010 11:59:59"];
    NSDate *currentDate = [dateformatter dateFromString:launchDay];

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    int unitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *components = [gregorian components:unitFlags fromDate:today toDate:currentDate options:0];
    countdownLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d:%02d", components.day, components.hour, components.minute, components.second ];

    if (today >= currentDate){

        UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@"Coming Soon" message:@"Feed goes live Friday 26th November 12:00pm" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }       
    else {
        UIAlertView*alert =[[UIAlertView alloc] initWithTitle:@"Perfect" message:@"It Works" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
        [alert release];        
    }    
}

Any help is greatly appreciated as I really am not sure how I am going wrong or what i should have in place.

Oh yes one last thing, when I run the App in the simulator and on device, it just randomly displays one of the messages, and I cannot determine what causes each message to display but it isn't the date!

Thanks guys! Phil

+2  A: 

When comparing dates you should use the comparison methods of NSDate The behaviour you are seeing is a result of comparing the memory addresses of today and currentDate

Change

if (today >= currentDate)

To

if ([today timeIntervalSinceDate:currentDate] <= 0 ) // today is before launch

Refer to the NSDate Class Reference for more detail

falconcreek
Thanks man, changed the code but still didn't work, changed the time section of the date to 11:59:00 instead of 11:59:59 and this seems to have fixed the problem!
Phil Hopper