Répétez UILocalNotification tous les jours à 17 heures

Comment répéter UILocalNotification tous les jours à 17 heures? Voici mon code pour définir l'heure personnalisée. Mais je veux informer l'user quotidiennement à la coutume ou peut être l'heure statique. J'utilise iOS 6.

-(void)scheduleNotification{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"h:mm a"]; NSDate *dateFromSsortingng = [[NSDate alloc] init]; dateFromSsortingng = [dateFormatter dateFromSsortingng:timeStr]; UILocalNotification *localNotif = [[UILocalNotification alloc] init]; if (localNotif == nil) return; localNotif.fireDate = dateFromSsortingng; localNotif.repeatInterval = kCFCalendarUnitDay; localNotif.timeZone = [NSTimeZone defaultTimeZone]; // Notification details localNotif.alertBody = @"Reminder is set"; // Set the action button localNotif.alertAction = @"Ok"; localNotif.soundName = UILocalNotificationDefaultSoundName; localNotif.applicationIconBadgeNumber = 1; // Specify custom data for the notification NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Local Push received while running", @"key", nil]; localNotif.userInfo = infoDict; // Schedule the notification [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; } 

Utilisez ceci. Cela peut vous aider

 NSDateFormatter *dat= [[NSDateFormatter alloc]init]; [dat setLocale:[NSLocale currentLocale]]; [dat setTimeZone:[NSTimeZone systemTimeZone]]; //[dat setDateFormat:@"YYYY-MM-dd"];// YYYY-MM-dd hh:mm a //NSSsortingng *dateM=[dat ssortingngFromDate:datM]; //[dat setDateFormat:@"YYYY-MM-dd h:mm a"]; NSDate *reminderDate=[NSDate date]; reminderDate =[reminderDate dateByAddingTimeInterval:1*24*60*60]; UILocalNotification *missingDreamNotify=[[UILocalNotification alloc]init]; missingDreamNotify.fireDate=reminderDate; missingDreamNotify.timeZone = [NSTimeZone defaultTimeZone]; missingDreamNotify.alertBody = @"Reminder is set"; missingDreamNotify.alertAction = @"Show me"; missingDreamNotify.soundName = UILocalNotificationDefaultSoundName; missingDreamNotify.applicationIconBadgeNumber = 1; missingDreamNotify.repeatInterval = NSDayCalendarUnit; [[UIApplication sharedApplication] scheduleLocalNotification:missingDreamNotify]; 

Voici ma solution, en utilisant NSCalendar:

 // called in didFinishLaunchingWithOptions /* cancel all local notifications at start up and reset them as repeating notifications. processDailyNotifications register to system cancel all for i in notifArray: setDailyNotificationAtHour:minute: setDailyNotificationAtHour:minute: */ - (void)processDailyNotifications { UIApplication *application = [UIApplication sharedApplication]; // are you running on iOS8? if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil]; [application registerUserNotificationSettings:settings]; } else // iOS 7 or earlier { UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [application registerForRemoteNotificationTypes:myTypes]; } [[UIApplication sharedApplication] cancelAllLocalNotifications]; [@[@{@"hour":@0,@"minute":@58},@{@"hour":@0, @"minute":@59}] enumerateObjectsUsingBlock:^(NSDictionary *item, NSUInteger idx, BOOL *stop) { [self setDailyNotificationAtHour:[[item valueForKey:@"hour"] integerValue] minute:[[item valueForKey:@"minute"] integerValue]]; }]; } - (void)setDailyNotificationAtHour:(NSInteger)hour minute:(NSInteger)minute { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDate *now = [NSDate date]; NSDate *expected = [calendar dateBySettingHour:hour minute:minute second:0 ofDate:now options:NSCalendarMatchSsortingctly]; UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.fireDate = expected; notification.alertAction = @"记录"; notification.alertBody = @"记录时间"; notification.soundName = UILocalNotificationDefaultSoundName; notification.applicationIconBadgeNumber = 1; notification.repeatInterval = NSDayCalendarUnit; ALog(@"%ld, %ld, %@", (long)hour, (long)minute, [expected descriptionWithLocale:[NSLocale currentLocale]]); [[UIApplication sharedApplication] scheduleLocalNotification:notification]; }