使用iOS 7获取后台步骤

前端之家收集整理的这篇文章主要介绍了使用iOS 7获取后台步骤前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个应用程序,在其中我应该获得我在体育活动中所做的步骤数.我找到了这段代码
- (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不允许在后台模式下使用加速度计(只有iPhone 5s和协处理器M7可以在显示关闭时获得加速度计值),我如何在后台模式下使用加速度计来计算步数?我想应该有一种方法,否则我无法理解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

原文链接:https://www.f2er.com/iOS/331835.html

猜你在找的iOS相关文章