2

ローカル通知を設定しましたが、問題はそれを土曜日に設定し、繰り返し間隔を 1 週間に設定したことです。ただし、正しい日付を設定しましたが、毎週日曜日に正しく設定された時間に取得します。誰かが私の間違いを見ますか?ああ、忘れないでください。正しい日を設定すると、... 水曜日であれば、1 分後にすぐに通知が届きます。どこが私のせいなのかわからない。

    - (void)applicationDidEnterBackground:(UIApplication *)applicatio
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;

    NSDateComponents *componentsForReferenceDate =

    [calendar components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit ) fromDate:[NSDate date]];

    //set day (saturday)

    [componentsForReferenceDate setDay:1] ;
    [componentsForReferenceDate setMonth:12] ;
    [componentsForReferenceDate setYear:2012] ;

    NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate] ;

    // set components for time 18:30. 

    NSDateComponents *componentsForFireDate =

    [calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit ) fromDate: referenceDate];

    [componentsForFireDate setHour: 18] ;
    [componentsForFireDate setMinute:38] ;
    [componentsForFireDate setSecond:0] ;

    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];

    // Create the notification

    UILocalNotification *notification = [[UILocalNotification alloc]  init] ;

    notification.fireDate = fireDateOfNotification ;
    notification.timeZone = [NSTimeZone localTimeZone] ;
    notification.alertBody = [NSString stringWithFormat: @"You are missed!"] ;
    notification.alertAction = @"Back";
    notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some information"] forKey:@"information"];
    notification.repeatInterval= NSWeekCalendarUnit ;
    notification.soundName = @"Appnotifisound.wav";
    notification.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification] ;

ありがとう。

4

1 に答える 1

0

これは、2 つの異なる を作成しているためですNSDateComponents。1 つは日付、月、年を設定し、もう 1 つは時間、分、秒を設定します。

これはうまくいくはずです:

- (void)applicationDidEnterBackground:(UIApplication *)application {

   [[UIApplication sharedApplication] cancelAllLocalNotifications];

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;    
    NSDateComponents *componentsForReferenceDate = [[NSDateComponents alloc] init];

    //set day (saturday)

    [componentsForReferenceDate setDay:7] ;
    [componentsForReferenceDate setMonth:12] ;
    [componentsForReferenceDate setYear:2012] ;

    // set time (08:30 PM) 

    [componentsForReferenceDate setHour: 20] ;
    [componentsForReferenceDate setMinute:30] ;
    [componentsForReferenceDate setSecond:00] ;

    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForReferenceDate];

    // Create the notification

    UILocalNotification *notification = [[UILocalNotification alloc]  init] ;

    notification.fireDate = fireDateOfNotification ;
    notification.timeZone = [NSTimeZone localTimeZone] ;
    notification.alertBody = [NSString stringWithFormat: @"You are missed!"] ;
    notification.alertAction = @"Back";
    notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some information"] forKey:@"information"];
    notification.repeatInterval= NSWeekCalendarUnit ;
    notification.soundName = @"Appnotifisound.wav";
    notification.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification] ;

}
于 2012-12-08T01:31:11.410 に答える