2012-04-22 4 views
4

Я использую Xcode 4.3.2, как я могу установить это локальное уведомление «dateToFire» каждый день в 6 утра?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. Метод класса '+ calendar' не найден (тип возврата по умолчанию - 'id') 2. Использование необъявленного идентификатора 'NSMinuteCalendarComponents'. – shebi

+0

NSMinuteCalendarComponents - это NSMinuteCalendarUnit, извините) –

+0

И календарь должен быть текущим календарем) –

Смежные вопросы