I am currently in the Process of building my first iOS Swift app, with this app I want to perform an action while the app is running in the background.
The action needs to be performed once the user taps twice on the device.
I've enabled Background Modes: Location updates in the app Capabilities
And setup a AccelerometerUpdatesToQueue function in the AppDelegate:
let manager = CMMotionManager()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
if manager.accelerometerAvailable {
manager.accelerometerUpdateInterval = 0.01
manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) {
[weak self] (data: CMAccelerometerData!, error: NSError!) in
println(data.acceleration.z)
}
}
return true
}
The console prints out the acceleration.z value as expected, but once I press the home button, the console prints stop.
I've searched the web for a sample code on how to do this, and I know it's possible... because we all know the app "Knock Knock to unlock", but I can't seem to find a piece of sample code for Swift.