4

Xcode 4.3.2を使用していますが、このローカル通知「dateToFire」を毎日午前6時に設定するにはどうすればよいですか?

-(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];
}
4

1 に答える 1

6

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];
于 2012-04-22T08:22:35.917 に答える