6

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.

James Z
  • 12,209
  • 10
  • 24
  • 44
svh1985
  • 199
  • 2
  • 9

3 Answers3

9

I got it to work! This is my solution, feel free to suggest improvements :)

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    let manager = CMMotionManager()
    var knocked : Bool = false
    let motionUpdateInterval : Double = 0.05
    var knockReset : Double = 2.0

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        if manager.deviceMotionAvailable {
            manager.deviceMotionUpdateInterval = motionUpdateInterval // seconds

            manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) {
                [weak self] (data: CMDeviceMotion!, error: NSError!) in


                if (data.userAcceleration.z < -0.7) || (data.userAcceleration.z > 0.7) { // Check if device is knocked

                    // Check for double knock
                    if self!.knocked == false {
                        // First knock
                        println("First Knock")
                        self!.knocked = true

                    }else{
                        // Second knock
                        println("Double Knocked")
                        self!.knocked = false
                        self!.knockReset = 2.0

                        // Action:
                    }
                }

                // Countdown for reset action (second knock must be within the knockReset limit)
                if (self!.knocked) && (self!.knockReset >= 0.0) {

                    self!.knockReset = self!.knockReset - self!.motionUpdateInterval

                }else if self!.knocked == true {
                    self!.knocked = false
                    self!.knockReset = 2.0
                    println("Reset")
                }

            }
        }

        return true
    }
svh1985
  • 199
  • 2
  • 9
  • Will need to use a [background mode](https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html). – Individual11 Mar 30 '16 at 02:20
  • @Individual11 how we can by using background mode still make the coreMotion work? can you give me more info please? and does CoreMotion work in Kill App state? thanks alot for helping. – Rawan Apr 26 '17 at 10:13
  • 1
    @svh1985 hi, how you can make ur app work in background state? I didn't get you very will in your answer, can you give me more details please? I faced the same problem, and I don't know how to solve it? And question please do you know in CoreMotion work if the app is killed? thanks for helping :) – Rawan Apr 26 '17 at 10:16
  • I never got it to work the way I fully wanted. There are ways to keep your app running in the background for things like network and bluetooth, but I didn't want to use that as a cheat if my app didn't actually need it. – Individual11 Apr 27 '17 at 13:34
1

Thanks for posting this. It's helping me out though I need to figure out how to separate this data from regular movement data. One suggestion... use fabs to get the absolute value of userAcceleration.z.

 if (data.userAcceleration.z < -0.7) || (data.userAcceleration.z > 0.7) { // Check if device is knocked

becomes

if (fabs(data.userAcceleration.z) > 0.7) { // Check if device is knocked
Nicholas K
  • 151
  • 1
  • 10
1

In Swift 5.1 this line:

manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) { [weak self] (data: CMDeviceMotion!, error: NSError!) in

Needs to be:

manager.startDeviceMotionUpdates(to: .main) { [weak self] (data, error) in
Theo Lampert
  • 1,012
  • 10
  • 10