2012-04-08 12 views
1

Ich habe ein Objekt, das Zeiten in einem NSArray als NSString speichert. Ich habe NSString gewählt, weil ich am Ende viel anstatt des NSDates zeige. Ich versuche eine UILocalNotification dafür einzurichten. Was ich getan habe ist:UILocalNotification wird sofort statt zur geplanten Zeit ausgelöst

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.timeZone = [NSTimeZone systemTimeZone]; 
dateFormatter.dateFormat = @"h:mm a"; 

for (NSString *s in _times) { 
    NSDate *date = [dateFormatter dateFromString:s]; 
    NSCalendar *calendar = [NSCalendar currentCalendar]; 
    NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) 
                fromDate:date]; 
    NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date]; 
    NSDateComponents *dateComps = [[NSDateComponents alloc] init]; 
    [dateComps setDay:[dateComponents day]]; 
    [dateComps setMonth:[dateComponents month]]; 
    [dateComps setYear:[dateComponents year]]; 
    [dateComps setHour:[timeComponents hour]]; 
    [dateComps setMinute:[timeComponents minute]]; 

    NSDate *itemDate = [calendar dateFromComponents:dateComps]; 

    NSLog(@"%@", [dateFormatter stringFromDate:itemDate]); 
    UILocalNotification *note = [[UILocalNotification alloc] init]; 
    note.alertAction = thePill.name; 
    note.alertBody = thePill.note; 
    note.fireDate = itemDate; 
    note.timeZone = [[NSCalendar currentCalendar] timeZone]; 

    if (note.fireDate == nil) { 
     NSLog(@"nil firedate"); 
    } 

//  note.repeatInterval = NSDayCalendarUnit; 

    NSData *data = [self archivePill:thePill]; 
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:data forKey:PILL_NOTIFICATION]; 
    note.userInfo = infoDict; 

    [[UIApplication sharedApplication] scheduleLocalNotification:note]; 
} 

Als ich die itemDate Zeit NSLog ist es, was ich will. Am Ende der Methode wird die lokale Benachrichtigung sofort zu diesem Zeitpunkt ausgelöst. Ich bin mir nicht sicher warum, weil mein Date die richtige Zeit ist, und ich dachte, ich habe das FireDate nur auf dieses Datum gesetzt? Vielen Dank.

+2

Aus der Dokumentation verwendet werden sollen - Wenn der angegebene Wert für 'fireDate' Null oder ein Datum in der Vergangenheit wird die Benachrichtigung sofort geliefert. –

+0

Check Wert von 'NSDate * date', ich denke, es ist ein vergangenes Datum oder kann im richtigen Format sein, was es zu" nil "macht. –

+0

@Crystal: Bitte drucken Sie die aktuellen Daten mit '[NSDate date]' aus und vergleichen Sie diese mit Ihrem 'itemDate'. Ich denke, dein 'itemDate' kann der Vergangenheit angehören. –

Antwort

4
// Fire notification immediately after 1 sec 
UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1]; 
localNotification.alertBody = @"Your alert message"; 
localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
4

Um eine Benachrichtigung feuern sofort Sie

presentLocalNotificationNow 

Statt

scheduleLocalNotification 
Verwandte Themen