iphone - 如何安排 UILocalNotification 每隔一天或每三天一次?

标签 iphone ios objective-c notifications uilocalnotification

我在我的应用程序中安排了每天上午 10:00 的 UILocalNotification。

我为此使用了以下代码

 NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;

NSDateComponents *componentsForReferenceDate =

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

[componentsForReferenceDate setDay:10] ;
[componentsForReferenceDate setMonth:10] ;
[componentsForReferenceDate setYear:2013] ;

NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate] ;

// set components for time 10:00 a.m.

NSDateComponents *componentsForFireDate =

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

[componentsForFireDate setHour:10] ;
[componentsForFireDate setMinute:0] ;
[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: @"Good Morning! Have a great day!"] ;
notification.alertAction = @"go back";
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some information"] forKey:@"information"];
notification.repeatInterval = NSDayCalendarUnit ;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;

效果非常好。 但现在我想安排 UILocalNotification 每隔一天和每三天一次。 那么如何做到这一点呢?

最佳答案

您无法自定义 UILocalNotification 的重复时间间隔。有很多问题与您的问题相同,而且几天前我也有同样的问题,但我找不到任何解决方案,所以请最好停止与之争斗。

您必须需要使用重复时间间隔

日历单位 指定日历单位,例如日和月。

enum {
   NSEraCalendarUnit = kCFCalendarUnitEra,
   NSYearCalendarUnit = kCFCalendarUnitYear,
   NSMonthCalendarUnit = kCFCalendarUnitMonth,
   NSDayCalendarUnit = kCFCalendarUnitDay,
   NSHourCalendarUnit = kCFCalendarUnitHour,
   NSMinuteCalendarUnit = kCFCalendarUnitMinute,
   NSSecondCalendarUnit = kCFCalendarUnitSecond,
   NSWeekCalendarUnit = kCFCalendarUnitWeek,
   NSWeekdayCalendarUnit = kCFCalendarUnitWeekday,
   NSWeekdayOrdinalCalendarUnit = kCFCalendarUnitWeekdayOrdinal,
   NSQuarterCalendarUnit = kCFCalendarUnitQuarter,
   NSWeekOfMonthCalendarUnit = kCFCalendarUnitWeekOfMonth,
   NSWeekOfYearCalendarUnit = kCFCalendarUnitWeekOfYear,
   NSYearForWeekOfYearCalendarUnit = kCFCalendarUnitYearForWeekOfYear
   NSCalendarCalendarUnit = (1 << 20),
   NSTimeZoneCalendarUnit = (1 << 21),
};
typedef NSUInteger NSCalendarUnit;

以上内容可以从Apple的文档中找到。另请阅读这个问题...

How to set Local Notification repeat interval to custom time interval?

有关详细信息,您只安排了最大本地通知64,因此,如果您有计划,想要为自定义重复时间间隔创建多个通知,请务必小心。

关于iphone - 如何安排 UILocalNotification 每隔一天或每三天一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19315891/

相关文章:

iphone - 奇怪的核心数据错误

objective-c - 如何使用 AVAssetReader 添加到 AudioBufferList?

ios - UITableView 无法到达新行

iphone - iphone中从屏幕底部向上滑动并有多种选择的元素叫什么?

iphone - 核心数据生成空 SQLite 数据库

iphone - objective-c UIAlert 按钮不起作用

iphone - 如何使用相同类型的子对象计算自定义对象的深度?

ios - UIScrollView内多个UITextView在分页模式交互问题

ios - 在 tableviewheader 中使用 UITextField 进行 self.view.endEditing 时崩溃

ios - dispatch_after 和 [UIView 动画 : duration] happen immediately (but shouldn't)