2

UILocalNotifications を 1 時間ごとに起動するアプリがあり、ユーザーが日付ピッカーで選択した時刻に停止します。時間コンポーネントを 1 つずつ追加して、23 のローカル通知を事前設定しました。問題は、時間が午前 12 時を過ぎると、その日は同じ日に留まり、変わらないことです。時間が午前 12:00 に達した日に NSDate を変更するにはどうすればよいですか? 助けてくれてありがとう。

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

NSDate *pickerDate = [self.datePicker date];

NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                               fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                               fromDate:pickerDate]; 

NSDateComponents *dateComps = [[NSDateComponents alloc] init];

[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:0];

//date chosen from the picker with seconds = 0 i.e., final alarm
NSDate *itemDate = [calendar dateFromComponents:dateComps];


//setting the final alarm

UILocalNotification *finalAlarm = [[UILocalNotification alloc] init];
if (finalAlarm) {
    finalAlarm.fireDate = itemDate;
    finalAlarm.timeZone = [NSTimeZone defaultTimeZone];
    finalAlarm.repeatInterval = 0;
    finalAlarm.soundName = UILocalNotificationDefaultSoundName;
    finalAlarm.alertBody = @"Test message...";
    [[UIApplication sharedApplication] scheduleLocalNotification:finalAlarm];
}  

//Formatting original date to isolate the current hour
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH"];

//Formatting original date to isolate the current minute
NSDateFormatter *minuteFormatter = [[NSDateFormatter alloc]init];
[minuteFormatter setDateFormat:@"mm"]; 

//creating a NSDateFormatter for readability
NSDateFormatter *yearMonthDay = [[NSDateFormatter alloc]init];
[yearMonthDay setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];


//isolate current date/hour/minute
NSDate *currentDate = [NSDate date];
NSString *currentHour = [dateFormatter stringFromDate:currentDate];
NSString *currentMinute = [minuteFormatter stringFromDate:currentDate];


NSString *currentForamttedDate = [yearMonthDay stringFromDate:currentDate];
NSLog(@"Current Date: %@",currentForamttedDate); 

//current hour int
int currentHourInt = [currentHour intValue];

//current minute int
int currentMinuteInt = [currentMinute intValue];    


//current hour plus 1
int firstAlarmHour = currentHourInt + 1;

//creating the first alarm
NSDateComponents *firstAlarm = [[NSDateComponents alloc]init];

[firstAlarm setDay:[dateComponents day]];
[firstAlarm setMonth:[dateComponents month]];
[firstAlarm setYear:[dateComponents year]];
[firstAlarm setHour:firstAlarmHour];
[firstAlarm setMinute:currentMinuteInt];
[firstAlarm setSecond:0];

//creating a date from the components
NSDate *firstAlarmDate = [calendar dateFromComponents:firstAlarm];

//setting the first alarm
[self scheduleNotificationForDate: firstAlarmDate finalAlarm:itemDate];

-(void) scheduleNotificationForDate: (NSDate*)date finalAlarm: (NSDate *)finalAlarmDate {


NSComparisonResult result = [finalAlarmDate compare:date];

    if (alarm) {
        alarm.fireDate = date;
        alarm.timeZone = [NSTimeZone defaultTimeZone];
        alarm.repeatInterval = 0;
        alarm.soundName = UILocalNotificationDefaultSoundName;
        alarm.alertBody = @"Test message...";
        [[UIApplication sharedApplication] scheduleLocalNotification:alarm];
    }
}
4

1 に答える 1

3

改善:

1 時間の日付コンポーネントを作成し、カレンダーを取得して、そのコンポーネントを以前に計算した (最初の) 開始日に追加します。

NSDateComponents *hourComponent = [[NSDateComponents alloc] init];
hourComponent.hour = 1;

dateToBeIncremented = [theCalendar dateByAddingComponents:hourComponent toDate:dateToBeIncremented options:0];

オリジナル(タイムゾーンに問題がある):

NSDate dateByAddingTimeInterval:日付コンポーネントを使用して最初の通知の日付を整理し、次に を使用して 1 日分の秒を加算して後続の日付を計算するのはどうでしょうか。

より良い代替案は、1 時間の討論コンポーネントを作成することです。最初の発火日を取得したら、時間の日付コンポーネントを使用して次の発火日を生成し、それを使用して次の発火日を生成します。これは、上記の方法よりも多くのタイムゾーン関連の問題に対処する必要があります。

于 2013-06-10T22:43:39.890 に答える