
当用户禁用通知时,将此次存储在NSUserDefaults中作为notificationsDisabledTime.
// Declare this constant somewhereconst Nsstring *kNotificationdisableTime=@"disable_notifications_time"[[NSUserDefaults sharedUserDefaults] setobject:[NSDate date] forKey:kNotificationdisableTime];
现在,只要应用程序启动或到达前台,请检查是否
notificationsDisabledTime和当前时间之间的持续时间大于一周.如果是,请重新启用通知.用一个很好的可重用函数包装它.在app delegate,applicationDIDBecomeActive中调用此函数:
-(voID)reenableNotificationsIfNecessary { if ( notifications are already enabled ... ) { return; } NSDate *DisabledDate = [[NSUserDefaults sharedUserDefaults] objectForKey:kNotificationdisableTime] NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIDentifIEr:NSGregorianCalendar]; NSUInteger unitFlags = NSDayCalendarUnit; NSDateComponents *components = [gregorian components:unitFlags fromDate:DisabledDate toDate:[NSDate date] options:0]; NSInteger days = [components day]; if(days >7) { // re-enable notifications }} 作为备份,有一个NSTimer每小时触发一次执行相同的检查,即调用此函数.这是为了处理用户在您的应用中花费大量时间的情况.这种方式在一周之后它最终将重新启用,但不一定恰好在正确的时间,但通常是正常的.
总结以上是内存溢出为你收集整理的在iOS应用中安排任务全部内容,希望文章能够帮你解决在iOS应用中安排任务所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)