Motion manager not working

I'm trying to get device movement (step, roll, yaw), but my handleDeviceMotionUpdate function does not start (I'm trying to do this on iphone 5s), here is the code:

import UIKit import CoreMotion class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() var motionManager = CMMotionManager() motionManager.startDeviceMotionUpdates() motionManager.deviceMotionUpdateInterval = 0.1 motionManager.startDeviceMotionUpdatesToQueue( NSOperationQueue.currentQueue()!, withHandler: { (deviceMotion, error) -> Void in if(error == nil) { self.DeviceMotionUpdate(deviceMotion!) } else { print("error") } }) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func DeviceMotionUpdate(deviceMotion:CMDeviceMotion) { print("function launched") var attitude = deviceMotion.attitude var roll = (attitude.roll) var pitch = (attitude.pitch) var yaw = (attitude.yaw) print("Roll: \(roll), Pitch: \(pitch), Yaw: (yaw)") } } 

I am not getting an error in startDeviceMotionUpdatesToQueue. I believe in motionManager.deviceMotionActive and in motionManager.deviceMotionAvailable

+5
source share
1 answer

Your motionManager will probably be destroyed after the viewDidLoad method completes. Try making the motionManager a property of the class.

 class ViewController: UIViewController { var motionManager = CMMotionManager() override func viewDidLoad() { super.viewDidLoad() self.motionManager.startDeviceMotionUpdates() ... 
+6
source

All Articles