Running a app forever in the background in iOS 8

2019-03-22 06:55发布

There are several thread on this topic and nothing which works for me. I don't have to deploy the app in the app store, so I can do hacks in the app to keep it running. Any way I can keep the app going even when it is backgrounded? Any pointers appreciated.

1条回答
小情绪 Triste *
2楼-- · 2019-03-22 07:34

I found this github project solving this problem: https://github.com/voyage11/Location Obviously Apple wouldn't approve this hack, but I guess it would work in your case.

Basically what you have to do is:

  • Use the location background mode capability in info.plist
  • Always have background task running, but don't let it run for longer than a minute. Create a new background task every minute and stop old task.
  • Apart from the previous task rolling, also keep a long running background task. I'm not sure though if that is really needed.
  • Start the location manager every minute and requestAlwaysAuthorization.

Some important snippets from the referenced code:

Background task:

bgTaskId = [application beginBackgroundTaskWithExpirationHandler:^{
    [self.service debugLog:[NSString stringWithFormat:@"BG....background task %lu expired", (unsigned long)bgTaskId]];
}];

And start the location manager:

if(IS_OS_8_OR_LATER) {
    [_locationManager requestAlwaysAuthorization];
}
[_locationManager startUpdatingLocation];
查看更多
登录 后发表回答