2012-04-22 101 views
4

我正在使用Xcode 4.3.2,如何在每天早上6点设置本地通知“dateToFire”?xcode - 按时间安排本地通知(每天早上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]; 
} 

回答

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]; 
+0

感谢您的快速回复,但它有一些错误。 1.未找到class method'+ calendar'(返回类型默认为'id')2.使用未声明的标识符'NSMinuteCalendarComponents'。 – shebi 2012-04-22 08:38:47

+0

NSMinuteCalendarComponents是NSMinuteCalendarUnit,对不起) – 2012-04-22 08:41:08

+0

和日历应该是currentCalendar) – 2012-04-22 08:41:30

相关问题