gpt4 book ai didi

ios - 使用 iOS 7 在后台获取步骤

转载 作者:可可西里 更新时间:2023-11-01 03:31:43 25 4
gpt4 key购买 nike

我正在开发一个应用程序,我应该在其中获取我在体育锻炼期间所走的步数。我找到这段代码:

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

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

最佳答案

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

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.

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

也看到这个,看起来很有趣 https://www.cocoacontrols.com/controls/somotiondetector

关于ios - 使用 iOS 7 在后台获取步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22094552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com