Task planning

I want to implement a function similar to the WhatsApp mute function. Thus, the user stops receiving notifications (in my case, using the location manager) for some time. After this time, notifications (location manager) are turned on automatically. How can I schedule such an event (Turn on the location manager automatically), for example 1 week after I click the button?

+8
ios swift
source share
2 answers

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) { // re-enable notifications } } 

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.

+6
source share

1. Approach I suggest using the NSTimer Class and setting a timer to receive a function call that will be enabled. And also the Background task for using the method in the background, and this can be done by adding

 var bgTask = UIBackgroundTaskIdentifier var app = UIApplication.sharedApplication() app.beginBackgroundTaskWithExpirationHandler { () -> Void in app.endBackgroundTask(bgTask) } 

before calling the schedule.

For example, I wanted to mute the sound for 8 hours, than you need to call

 NSTimer.scheduledTimerWithTimeInterval(60*8, target:(self), selector: Selector("stopper"), userInfo: nil, repeats: no); 

and add stopper

 func stopper(){ //unmute } 

You can also send certain information about the object that will be disabled by adding userInfo to the timer.

2. Approach

You can see the time difference between applicationDidEnterBackground and applicationDidEnterForeground

let date = NSDate.date() and let difference NSDate.timeIntervalSinceDate(date)

+1
source share

All Articles