0

I am trying to be able to set how long my app will remain in the background. I want to be able to have the control over how long my app can stay active in the background until it is closed. Is there something I can add to the Info.plist that can set that, with the bool that is the switch to have it run in the background or not?

I have read and found that Apple doesn't let apps remain in the background for longer than 10min, but I have done some testing and found other live apps that are similar to mine, last longer than 10min. I have also found old posts that bypass this by setting and reseting timers before the 10min mark, does this still work with iOS7 and does Apple still accept it?

1 Answer 1

3

Edit: Misunderstood the questions. Here is a possible alternative.

- (void)applicationDidEnterBackground:(UIApplication *)application{
      UIApplication*    app = [UIApplication sharedApplication];
      task = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:task];
        task = UIBackgroundTaskInvalid;
    }];
    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // Do the work associated with the task.
        NSLog(@"Started background task timeremaining = %f", [app backgroundTimeRemaining]);
        // save time to NSUserdefaults
        [[NSUserdefaults standardUserDefaults] setObject:[NSDate date] objectForKey@"startDate"];

        [app endBackgroundTask:task];
        task = UIBackgroundTaskInvalid;
    });

}

- (void)applicationWillTerminate:(UIApplication *)application{
    [[NSUserdefaults standardUserDefaults] setObject:[NSDate date] objectForKey@"terminateDate"];
}

-(void)applicationDidFinishLaunching:(UIApplication)application{
   //get start time & terminate time & then take a diffrence
   NSDate* temporaryDate1 = (NSDate*)[userDefaults objectForKey:@"startDate"];
   NSDate* temporaryDate2 = (NSDate*)[userDefaults objectForKey:@"terminateDate"];
   compare[temporaryDate1,temporaryDate2];
}

Here is a link to check how to compare NSDate instance.

Sign up to request clarification or add additional context in comments.

3 Comments

He wants to be able to set the time the app runs in the background, not how to check how long it has been in the background.
Looks good, but does this keep the app from being closed down after the 10min of being in the background as well? Or does that not happen anymore?
Nope this will only tell you if apple lied you or not !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.