我试图收集coreMotion加速数据在后台超过10分钟。这必须是可能的,因为应用程序,如睡眠周期,这样做。
不过,我只想确保这是允许的,因为它似乎并不是其中之一:
Apps that play audible content to the user while in the background, such as a music player app
Apps that record audio content while in the background.
Apps that keep users informed of their location at all times, such as a navigation app
Apps that support Voice over Internet Protocol (VoIP)
Apps that need to download and process new content regularly
Apps that receive regular updates from external accessories
Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.然而,我已经尝试了以下步骤来获得一个背景任务,但是我认为CoreMotion有一个更好的方法:
标题:
UIBackgroundTaskIdentifier bgTask; 代码:
// if the iOS device allows background execution,
// this Handler will be called
- (void)backgroundHandler {
NSLog(@"### -->VOIP backgrounding callback");
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (1) {
NSLog(@"BGTime left: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
[self doSomething];
sleep(1);
}
});
- (void)applicationDidEnterBackground:(UIApplication *)application {
BOOL backgroundAccepted = [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{ [self backgroundHandler]; }];
if (backgroundAccepted)
{
NSLog(@"VOIP backgrounding accepted");
}
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (1) {
NSLog(@"BGTime left: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
[self doSomething];
sleep(1);
}
});
}发布于 2013-12-24 20:08:50
用这个:
Apps that keep users informed of their location at all times, such as a navigation app换句话说,您可以创建一个位置管理器,并告诉它开始进行更新。你不需要对那些更新做任何事情!但只要这种情况发生--也就是说,只要你的应用程序继续在后台进行位置更新--你的应用程序也可以在后台使用核心运动。这不仅仅是一个骗局,正如几年前WWDC的一段视频所解释的,这是苹果的官方政策。
https://stackoverflow.com/questions/20766139
复制相似问题