0

i make a sample app for my requirement the code is given below

design : contain 1 Button 1 Label

enter image description here

.h file code

@interface ViewController : UIViewController{
    int count;
    NSTimer *theTimer;
    UIBackgroundTaskIdentifier counterTask;
}

@property (weak, nonatomic) IBOutlet UILabel *theCount;

.m file code

- (IBAction)start:(id)sender {

    counterTask = [[UIApplication sharedApplication]
                   beginBackgroundTaskWithExpirationHandler:^{
                       // If you're worried about exceeding 10 minutes, handle it here

                       theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
                                                                 target:self
                                                               selector:@selector(countUp)
                                                               userInfo:nil
                                                                repeats:YES];

                   }];
    count=0;
    theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
                                              target:self
                                            selector:@selector(countUp)
                                            userInfo:nil
                                             repeats:YES];

}

- (void)countUp {
    if (count==100000) {
        [theTimer invalidate];
        [[UIApplication sharedApplication] endBackgroundTask:counterTask];
    } else {
        count++;
        NSString *currentCount;
        currentCount=[[NSString alloc] initWithFormat:@"%d",count];
        _theCount.text=currentCount;
    }
}

the problem is that when running this app in background (by pressing the home button and minimise the app in iPhone) it relaunch after 180 second even by enabling the background modes in capabilities . i need to extend this up to 4 hours. please help me.

Ben Rockey
  • 920
  • 6
  • 23
  • If your app has `bluetooth-central` background capability, your app will be awaken briefly when the device delivers an update. You cannot keep the app alive perpetually to do other things, only 10 seconds to process the update. Now, clearly, if the user wakes the device four hours later, your app should have received and processed all updates that happened since then. But this timer approach is a non-starter. – Rob Feb 17 '18 at 16:29

1 Answers1

0

The limit for background tasks is 3 minutes (180 seconds) in later versions of iOS. You cannot extend this to 4 hours.

Apple Docs:

Note: Always provide an expiration handler when starting a task, but if you want to know how much time your app has left to run, get the value of the backgroundTimeRemaining property of UIApplication.

Good Stack Overflow Post on the topic:

How long does Apple permit a background task to run?

Jake
  • 13,097
  • 9
  • 44
  • 73
  • https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html ( Implementing Long-Running Tasks ) - Apps that receive regular updates from external accessories this is my requirement. – Ben Rockey Feb 17 '18 at 05:08
  • where do you think I got the above quote...? – Jake Feb 17 '18 at 05:09
  • Just because you are using external accessories, doesn't mean iOS is going to let you run background tasks whenever you want. Check out the External Accessory Framework in iOS if you need to communicate with an external accessory while in the background. – Jake Feb 17 '18 at 05:13
  • thanks for the answer. in my project i need to connect with a BLE MedicalTreatment Device and set the device time from 30 min to 4 hr. and start through my app, after that a timer need to run on app screen timer passes to next screen when it complete. but in background app restart from beginning after 180 sec. what should i do.? – Ben Rockey Feb 17 '18 at 05:19
  • when the app comes back to the foreground, check the time and if's its been 4 hours, do what you need to do... – Jake Feb 17 '18 at 05:22
  • but if the user put the app on background more than 3 min it will relaunch how to avoid that.? – Ben Rockey Feb 17 '18 at 05:28
  • 2
    don't run a task in the background that goes past the allowed background time. you don't need a timer to see it's been 4 hours. save the time locally when the time should start and check the current time against the saved time when the app comes back in the foreground. – Jake Feb 17 '18 at 05:30