使用 iOS 7 在后台获取步骤 [英] Get the steps in background with iOS 7

查看:22
本文介绍了使用 iOS 7 在后台获取步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,我应该在其中获取我在体育活动中所走的步数.我找到了这个代码:

- (void)countSteps {[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/KUPDATEFREQUENCY];[[UIAccelerometer sharedAccelerometer] setDelegate:self];px = py = pz = 0;步数 = 0;self.labelSteps.text = [NSString stringWithFormat:@"%d", numSteps];}- (void)accelerationometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {浮动 xx = 加速度.x;浮动 yy = 加速度.y;浮动 zz = 加速度.z;浮动点 = (px * xx) + (py * yy) + (pz * zz);浮动 a = ABS(sqrt(px * px + py * py + pz * pz));浮动 b = ABS(sqrt(xx * xx + yy * yy + zz * zz));点/= (a * b);如果(点<= 0.82){如果 (!isSleeping) {isSleeping = 是;[self performSelector:@selector(wakeUp) withObject:nil afterDelay:0.3];numSteps += 1;self.labelSteps.text = [NSString stringWithFormat:@"%d", numSteps];}}像素 = xx;py = yy;pz = zz;}- (void)wakeUp {isSleeping = NO;}

使用此代码,当 iPhone 的显示屏打开时,它工作得很好,但是当我关闭显示屏时,它就不再工作了.为了跟踪我在 iOS 7 中看到的位置,有一个背景模式"功能.使用此功能,我可以在 iPhone 显示屏关闭时获取坐标.现在我必须在显示器关闭时获取加速度计值,我该怎么做?我在网上读到 iOS 不允许在后台模式下使用加速度计(只有带有协处理器 M7 的 iPhone 5s 可以在显示屏关闭时获取加速度计值),我如何通过在后台模式下使用加速度计来计算步数?我想应该有一种方法,否则我无法理解 Moves 应用程序 是如何工作的.

解决方案

当显示屏关闭时,您无法访问加速度计,但无论 iPhone 型号如何,即使显示屏关闭,您也可以监控位置变化.根据我的猜测,Moves 应用程序使用基于地理位置的更改来计算步数.在 CLLocationManger 文档中,我阅读了这个

<块引用>

在 iOS 6 及更高版本中,您可以在以下情况下延迟位置数据的交付您的应用程序在后台.建议你使用这个在您的应用程序可以稍后处理数据的情况下的功能没有任何问题.例如,跟踪用户的应用程序远足路径上的位置可能会推迟更新,直到用户远足一定距离,然后一次性处理所有点. 推迟更新通过允许您的应用程序保持睡眠状态来帮助节省电量更长的时间.

- (void)allowDeferredLocationUpdatesUntilTraveled:(CLLocationDistance)distance timeout:(NSTimeInterval)timeout

<块引用>

如果您的应用在后台并且系统能够优化它的耗电量,位置管理器告诉 GPS 硬件存储内部新位置,直到指定的距离或超时条件满足.当满足一个或两个条件时,位置经理通过调用locationManager:didFinishDeferredUpdatesWithError: 其方法将缓存的位置委托并传递给locationManager:didUpdateLocations: 方法.如果您的应用程序在前台,位置管理器不会推迟事件的传递但确实监视指定的标准.如果您的应用程序移至在满足条件之前,位置经理可以开始推迟事件的交付.

因此,当您获得一组缓存位置时,您可以计算发生的大致步骤数.我只是把我所有的疯狂想法都给了,这取决于你!

也看这个,好像很有趣https://www.cocoacontrols.com/controls/somotiondetector

I'm developing an app in which I should get the number of steps I made during a physical activity. I found this code:

- (void)countSteps {
    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0 / KUPDATEFREQUENCY];
    [[UIAccelerometer sharedAccelerometer] setDelegate:self];

    px = py = pz = 0;
    numSteps = 0;

    self.labelSteps.text = [NSString stringWithFormat:@"%d", numSteps];
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    float xx = acceleration.x;
    float yy = acceleration.y;
    float zz = acceleration.z;

    float dot = (px * xx) + (py * yy) + (pz * zz);
    float a = ABS(sqrt(px * px + py * py + pz * pz));
    float b = ABS(sqrt(xx * xx + yy * yy + zz * zz));

    dot /= (a * b);

    if (dot <= 0.82) {
        if (!isSleeping) {
            isSleeping = YES;
            [self performSelector:@selector(wakeUp) withObject:nil afterDelay:0.3];
            numSteps += 1;
            self.labelSteps.text = [NSString stringWithFormat:@"%d", numSteps];
        }
    }
    px = xx;
    py = yy;
    pz = zz;
}

- (void)wakeUp {
    isSleeping = NO;
}

With this code when the display of the iPhone is on it works great, but when I switch off the display it doesn't work anymore. To tracking the position I saw that in iOS 7 there's a function "background mode". With this function I can get the coordinates when the iPhone display is turned off. Now I've to get the accelerometer values when the display is turned off, how I can do that? I read on the web that iOS doesn't permit to use accelerometer in background mode (only iPhone 5s with the coprocessor M7 can get the accelerometer value when the display is turned off), how I can count the steps by using accelerometer in background mode? I guess there should be a way otherwise I can't understand how Moves app works.

解决方案

You cannot access the accelerometer when the display is turned off but you can monitor for location changes even when the display is off regardless of the iPhone model. According to my guess Moves app uses geolocation based changes to count steps. In the CLLocationManger docs, I read this

In iOS 6 and later, you can defer the delivery of location data when your app is in the background. It is recommended that you use this feature in situations where your app could process the data later without any problems. For example, an app that tracks the user’s location on a hiking trail could defer updates until the user hikes a certain distance and then process the points all at once. Deferring updates helps save power by allowing your app to remain asleep for longer periods of time.

- (void)allowDeferredLocationUpdatesUntilTraveled:(CLLocationDistance)distance timeout:(NSTimeInterval)timeout

If your app is in the background and the system is able to optimize its power usage, the location manager tells the GPS hardware to store new locations internally until the specified distance or timeout conditions are met. When one or both criteria are met, the location manager ends deferred locations by calling the locationManager:didFinishDeferredUpdatesWithError: method of its delegate and delivers the cached locations to the locationManager:didUpdateLocations: method. If your app is in the foreground, the location manager does not defer the deliver of events but does monitor for the specified criteria. If your app moves to the background before the criteria are met, the location manager may begin deferring the delivery of events.

So when you get a set of cached locations you could calculate the approximate number of steps that happened. I am just giving all my wild thoughts, its up to you!

See this also, seems interesting https://www.cocoacontrols.com/controls/somotiondetector

这篇关于使用 iOS 7 在后台获取步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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