I would suggest a hybrid approach using both NSTimers and validation whenever an application starts or comes to the fore.
When a user disables notifications, save this time in NSUserDefaults as notificationsDisabledTime.
// Declare this constant somewhere const NSString *kNotificationDisableTime=@"disable_notifications_time" [[NSUserDefaults sharedUserDefaults] setObject:[NSDate date] forKey:kNotificationDisableTime];
Now that the application launches or comes to the fore, check the duration between the DisabledTime notifications and the current time for more than one week. If so, activate notifications. Wrap this in a good reusable feature. Call this function in the application 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) {
As a backup, you have NSTimer that fires approximately every hour, performing the same check, that is, calling this function. This applies when a user spends a lot of time in your application. Thus, in a week it will be turned on again, although it is not necessary EXACTLY at the right time, but this is normal.
Samhan salahuddin
source share