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

我试图在后台收集coreMotion加速数据超过10分钟.这一定是可能的,因为像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,^{

    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视频中解释.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...