Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift CoreMotion detect Tap or Knock on device while in background

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.

like image 498
svh1985 Avatar asked Jun 03 '15 12:06

svh1985


3 Answers

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
    }
like image 94
svh1985 Avatar answered Sep 27 '22 19:09

svh1985


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
like image 44
Nicholas K Avatar answered Sep 27 '22 19:09

Nicholas K


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
like image 41
Theo Lampert Avatar answered Sep 27 '22 20:09

Theo Lampert