How Nike+ GPS on iPhone receives accelerometer updates in the background?

后端 未结 2 884
轻奢々
轻奢々 2020-11-28 03:49

The new Nike+ GPS application for iOS 5 is able to process accelerometer events in the background (thus allowing for indoor treadmill tracking). How is this possible? When I

相关标签:
2条回答
  • 2020-11-28 04:04

    When the the block with update of pedometer, I call the method to turn Off and turn On the GPS. For me it fixed the problem when the user press home and stay for one hour listening music on spotify.

    Don't forget to enable Project Settings -> Capabilities -> Background modes - > Location updates

        import CoreLocation
        import CoreMotion
    
    
        //turn Off and turn On GPS
        private func apelacaoRenovarGPSLigado() {
    
        LocationManager.sharedInstance.stopUpdatingLocation()
        let locationManager = LocationManager.sharedInstance
        locationManager.autoUpdate = true
        locationManager.startUpdatingLocationWithCompletionHandler { (location, status, verboseMessage, error) -> () in
    
        }
    }
    
    private func startPedometer()
    {
        if CMPedometer.isDistanceAvailable()
        {
            if self.pedometer == nil
            {
                self.pedometer = CMPedometer()
            }
    
            self.pedometer!.startPedometerUpdatesFromDate(NSDate()) { (data, error) in
    
                dispatch_async(dispatch_get_main_queue(), {
    
                    print("DISTANCIA PARCIAL \(data!.distance!)")
    
                    print("DIFERENCA \(data!.distance!.doubleValue - (self.totalDistance + self.subtotalDistance))")
                    self.delegate!.trackerDistanceDidChanged((data!.distance!.doubleValue - (self.totalDistance + self.subtotalDistance)) / 1000.0)
                    self.subtotalDistance = data!.distance!.doubleValue
    
                    self.apelacaoRenovarGPSLigado()
    
                })
    
            }
        }
        else
        {
            self.delegate!.trackerDeviceNotSupported()
        }
    }
    
    0 讨论(0)
  • 2020-11-28 04:05

    For the sake of providing an answer to this question, even though it is already self answered...

    "If you use the newer Core Motion API, you can receive updates in the background."

    Here is an example:

    - (void)startAccelerationCollection {
        [self.motionManager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
                                                 withHandler:^(CMAccelerometerData *data, NSError *error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.accelerometerReadings addObject:data];
            });
        }];
    }
    
    0 讨论(0)
提交回复
热议问题