2012-01-03 2 views
2

Я хочу, чтобы уведомление в 10 вечера (независимо от страны) было отправлено каждую неделю. Нужно ли использовать timeZone. В настоящее время я использую ниже код для уведомления об увольнении каждую неделю, но там, как предупредить уведомление точно в 10 вечера.Местное уведомление iPhone в определенное время

NSDate *date = [NSDate date]; 
NSDate* myNewDate = [date dateByAddingTimeInterval:7*24*60*60];  
UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
localNotification.fireDate = myNewDate; 
localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
localNotification.repeatInterval = NSWeekCalendarUnit; 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
[localNotification release]; 

ответ

4

Я думаю, что установка вашего fireDate до 22:00 на следующий день вы хотите получить уведомление (а не «через неделю») будет делать то, что вы хотите. Вероятно, вы захотите использовать NSCalendar.

NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; 
NSDate *currentDate = [NSDate date]; 
NSDate *fireDate = nil; 

[dateComponents setDay:3]; // ...or whatever day. 
[dateComponents setHour:22]; 
[dateComponents setMinute:0]; 

fireDate = [gregorian dateByAddingComponents:dateComponents 
             toDate:currentDate 
            options:0]; 

[localNotification setFireDate:fireDate]; 
[dateComponents release]; 
+0

Спасибо за логику ... –

0

Я сделал эти изменения, и она работала ...

NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; 
NSDate *currentDate = [NSDate date]; 
NSDate *fireDate = nil; 

//Getting current Hour 
NSDateComponents *components = [[NSCalendar currentCalendar] components:(kCFCalendarUnitHour | kCFCalendarUnitMinute) fromDate:currentDate]; 
NSInteger hour = [components hour]; 
NSInteger minute = [components minute]; 

[dateComponents setDay:7];//After 1 week 
[dateComponents setHour:22-hour-1]; //Calculating Remaining time for 10PM && "-1" because i am adding 60 min 
[dateComponents setMinute:60-minute]; 

//Adding remaining time to current Time 
fireDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents 
               toDate:currentDate 
              options:0]; 
[localNotification setFireDate:fireDate]; 
Смежные вопросы