iPhone 在后台收集 CoreMotion 数据.(超过10分钟) [英] iPhone collecting CoreMotion data in the background. (longer than 10 mins)

查看:16
本文介绍了iPhone 在后台收集 CoreMotion 数据.(超过10分钟)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在后台收集超过 10 分钟的 coreMotion 加速度数据.这一定是可能的,因为 Sleep Cycle 之类的应用可以做到这一点.

I am trying to collect coreMotion acceleration data in the background for longer than 10 minutes. This must be possible since apps like Sleep Cycle do this.

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

I just want to make sure this is allowed though, since it does not seem to be one of these:

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 有更好的方法:

However, I have tried following these steps to get a background task, but I am thinking there is a better way for 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 的官方政策.

In other words, you make a location manager and tell it to start doing updates. You don't have to do anything with those updates! But as long as this is happening - that is, as long as your app is continuing to do location updates in the background - your app is also allowed to use Core Motion in the background. This is not just a trick; it is official Apple policy as explained in one of the WWDC videos from a couple of years ago.

这篇关于iPhone 在后台收集 CoreMotion 数据.(超过10分钟)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆