我正在使用 Xcode 4.3.2,如何在每天早上 6 点设置此本地通知“dateToFire”?

-(void)notification
{
    UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease];

    if (!localNotification)
        return;

    // Current date
    NSDate *date = [NSDate date];
    NSDate *dateToFire = //Everyday: 6AM;

    // Set the fire date/time
    [localNotification setFireDate:dateToFire];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
    [localNotification setAlertBody:@"Notification" ];

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self notification];
}

最佳答案

使用 CalendarComponents 将小时设置为 6,并将 localNotification.repeatInterval 设置为 NSDayCalendarUnit

NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
[components setHour:18];
[components setMinute:0];

localNotification.fireDate = [calendar dateFromComponents:components];

关于iphone - xcode - 随时间安排的本地通知(每天早上 6 点),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10266336/

10-10 09:31