且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

继续在后台运行iOS应用,从加速器收集数据并将其发送到服务器

更新时间:2023-01-24 08:50:03

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

}