Fire UILocalNotification in a specific Date

前端 未结 3 1438
太阳男子
太阳男子 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:02

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit fromDate:[NSDate date]];
    
    [components setHour:4];
    [components setMinute:0];
    
    NSDate *fireDate = [gregorian dateFromComponents:components];
     NSLog(@"Fire date : %@",fireDate);// Today 4pm is already passed. So you wont get notification 
    
    // You need to check if the time is already passed
    if ([fireDate compare:[NSDate date]] == NSOrderedAscending)
    {
        // Schedule it for next day 4pm
        components.day = components.day+1;
        fireDate = [gregorian dateFromComponents:components];
    }
    NSLog(@"Fire date : %@",fireDate);
    

提交回复
热议问题