Fire UILocalNotification in a specific Date

前端 未结 3 1417
太阳男子
太阳男子 2021-01-16 10:32

I want to fire a UILocalNotification in a specific Date. If I use this code:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentif         


        
3条回答
  •  悲哀的现实
    2021-01-16 11:11

    Try following code with 24 hour format:-

    NSDate *date = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];
    [components setHour:16];
    [components setMinute:0];
    [components setSecond:0];
    
    NSDate *newDate = [gregorian dateFromComponents: components];
    [gregorian release];    
    
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = newDate;
    localNotif.alertBody = [NSString stringWithFormat:@"Alarm for time : %@",newDate];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];
    

提交回复
热议问题