jaqueamo
jaqueamo

Reputation: 33

Core Motion not working in Xcode 7 Swift 2.0

I get no output from Core Motion regardless I use pull or push method. I'm using Xcode 7.0 beta 6 with Swift 2.0 on iPhone 4s iOS 8.4.

    let manager = CMMotionManager()

    manager.accelerometerUpdateInterval = 0.3
    manager.startAccelerometerUpdates()
    print(manager.accelerometerData?.acceleration.x)

    manager.accelerometerUpdateInterval = 0.3
    manager.startAccelerometerUpdatesToQueue(NSOperationQueue()) { (data: CMAccelerometerData?, error: NSError?) in
        guard data != nil else {
            print("There was an error: \(error)")
            return
        }
       print(data!.acceleration.x)
    }

Anybody knows what I'm doing wrong? Thanks.

Upvotes: 2

Views: 1199

Answers (1)

RoberRM
RoberRM

Reputation: 883

Funny thing... I came here looking for answers to this very same question. ;)

As far as I can tell (I am no expert in Swift or XCode) the problem is that there is not enough time between instructions for core-motion to get the data: if you call manager.startAccelerometerUpdates() and then, after a while, ask for the accelerometerData, then you'll have no problem. If in my SpriteKit game I start accelerometer updates at the viewDidLoad method of a GameViewController and I ask for the accelerometer data at the didMoveToView method of a Scene, then I get the data. But if I start the accelerometer and ask for the data in the same method, then I get nothing (I get nothing also when I start the accelerometer right before a transition to other Scene and then ask for the data in the next Scene).

I don't know if this is normal or a bug, but at least I found a temporal solution: give core-motion as much time as possible before getting the data.

Upvotes: 4

Related Questions