Keep running the iOS app on background, collecting data from accelerator and send it to server

孤者浪人 提交于 2019-12-11 02:19:04

问题


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];
 }];

}


来源:https://stackoverflow.com/questions/27610170/keep-running-the-ios-app-on-background-collecting-data-from-accelerator-and-sen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!