2012-01-03 11 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

私は(「今から一週間」のではなく)あなたが通知を取得したい日には夜10時までごfireDateを設定すると、あなたがやりたいと思うだろう。これを行うには、おそらく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]; 
関連する問題