Keep running the iOS app on background, collecting

2019-07-21 13:58发布

问题:

I've developed an app, for reading the Accelerator data, store them locally and send them frequently with a specific interval to a server. The app must work on background, but after a while ( 3 minutes sometime ) it stops sending like it's closed.

Is it possible to keep the app alive all the time, running and sending data to a server on background? how ?

The app is supposed to keep working for weeks.

Thanks guys.

回答1:

iOS will allow you 3 minutes of execution in background. 

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

If you want to keep running there are various "workarounds" but not a stable solution.You can initiate background tasks and then manage them

 -(void)applicationDidEnterBackground:(UIApplication *)application

{ 
  UIApplication *app = [UIApplication sharedApplication];

    UIBackgroundTaskIdentifier bgTask = 0;

    backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self              selector:@selector(backgroundTask) userInfo:nil repeats:YES];

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
   [app endBackgroundTask:bgTask];
 }];

}