ios - iPhone 在后台收集 CoreMotion 数据。 (超过10分钟)

标签 ios iphone ios7 core-motion

我试图在后台收集超过 10 分钟的 coreMotion 加速数据。这一定是可能的,因为 Sleep Cycle 等应用可以做到这一点。

我只是想确保这是允许的,因为它似乎不是其中之一:

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

最佳答案

使用这个:

Apps that keep users informed of their location at all times, such as a navigation app

换句话说,您创建了一个位置管理器并告诉它开始进行更新。您无需对这些更新执行任何操作!但只要这种情况发生——也就是说,只要你的应用程序继续在后台进行位置更新——你的应用程序也被允许在后台使用 Core Motion。这不仅仅是一个技巧;正如几年前的一个 WWDC 视频中所解释的那样,这是 Apple 的官方政策。

关于ios - iPhone 在后台收集 CoreMotion 数据。 (超过10分钟),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20766139/

相关文章:

UITableViewCell 未正确提交编辑样式 [iOS7]

ios - 如何将 Dropbox 共享链接(针对 zip 文件)下载到本地

ios - 我应该使用 UITableView 节标题还是静态 UITableViewCell?

iphone - Google map 无法在 iPhone 模拟器上运行

iphone - 创建笔记 View 的最佳方法

ios - 没有出现iOS 7 Google AdMob

ios - 无法在 Xcode 8.3.2 中为 iOS 应用签署证书

ios - NSDate 中的时间/日期显示就像 iOS 中的消息和邮件应用程序一样

iPhone - 关于调度计时器和计时的问题

ios-simulator - iOS 7 模拟器错误 - NSAttributedString 没有出现