Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheduling a task in an iOS app

Tags:

ios

I want to implement a feature similar to WhatsApp's mute feature. So basically, user stops getting notifications (in my case, using Location Manager) for some time. After that time, notifications (Location Manager) is turned on automatically. How can I schedule such an event (Turning on location manager automatically) for example 1 week after I click a button?

like image 436
Arda Keskiner Avatar asked Aug 27 '15 11:08

Arda Keskiner


2 Answers

I would suggest a hybrid approach using both NSTimers and a check whenever the app launches or comes to the foreground.

When the user disables notifications store this time in NSUserDefaults as notificationsDisabledTime.

// Declare this constant somewhere
const NSString *kNotificationDisableTime=@"disable_notifications_time"

[[NSUserDefaults sharedUserDefaults] setObject:[NSDate date] forKey:kNotificationDisableTime];

Now whenever the app is launched or comes to the foreground , check whether duration between notificationsDisabledTime and current time is greater than one week. If so re-enable the notifications. Wrap this up in a nice reusable function. Call this function in 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
    }
}

As a backup , have an NSTimer that fires about once every hour performing the same check , ie calling this function. This is to handle the case where the user spends a lot of time in your app. This way after one week it will be re-enabled eventually , though not necessarily at EXACTLY the right time but that's alright usually.

like image 72
Samhan Salahuddin Avatar answered Nov 15 '22 17:11

Samhan Salahuddin


1. Approach I suggest to use NSTimer Class and set a timer to get call to function which will unmute. And also Background Task for using method in the background and it could be done by adding

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

before calling schedule.

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

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

and add your stopper

func stopper(){
   //unmute
}

also you can send specific info about the object which will be muted by adding userInfo to timer..

2. Approach

You can look to time differences between applicationDidEnterBackground and applicationDidEnterForeground

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

like image 36
mert Avatar answered Nov 15 '22 19:11

mert