If you’ve ever worked with the CoreMotion Framework, you’d probably agree, that its a curse. Apple puts too many restrictions, and provides various guidelines for the developers who want to leverage the power of sensors into their apps. Everybody knows that sensor enabled apps are power hungry , and many apps are rejected every year just because of the fact that they weren’t able to follow the proper guidelines, thus decreasing the battery performance drastically.

Same thing happened to me as well, a while ago. I recently joined Messiah as a Cofounder. Messiah is actually an emergency alert messenger, truly open sourced, to relay distress signal for the user in the case of an accident or emergency. Accident Detection was the core feature of this app, which needed some heavy computations with the data retrieved from some sensors like Accelerometer, Gyroscope and services like Device Motion. With the passage of time, our algorithm got pretty complex and it became really hard to maintain the code, specially the asynchronous calls of the CoreMotion Framework and the object lifecycle of the CMMotionManager throughout the app. So, long story short, I decided to make my own wrapper around the CoreMotion Framework, which just works and delivers what it says.

I named my wrapper, the MotionKit. MotionKit makes it easy for the developers to focus just on the logic, instead of dwindling around the core syntax and the object life-cycle stuff, having the performance metric in mind. You can do pretty much everything with it, i.e. you can retrieve all the sensor data with either push or pull method, recieve sensor data updates by using the powerful Trailing Closures or through the provided Delegates, use the helper methods which are included into the Kit, get the refined and processed data through the Device Motion service of the iOS which uses various sensor fusion algorithms to reduce the noise from the raw sensor data, and much much more.

How to install ?

Just go Here, clone the repository or Download it. Having that done, just copy the MotionKit.swift file into your Xcode project. Thats it. Now grab a cup of coffee, and see how the magic is been done.

Usage ( My favourite part )

The Magic Part is here.

First, initialise the MotionKit instance. Its a Must.

let motionKit = MotionKit()

Getting Accelerometer Data

Now lets say you wanna retrieve the Raw Accelerometer data from the MotionKit. Well, you can do this with just ‘two’ lines of code (without the Braces and formal stuff of course).

motionKit.getAccelerometerValues(interval: 1.0){

(x, y, z) in

//Do whatever you want with the x, y and z values

println("X: \(x) Y: \(y) Z \(z)")

}

Getting Gyroscope Data

Wanna use the Gyroscope ? Well, we’ve got you covered. Getting the raw Gyroscope data is as easy as getting a pie (and eating it too!)

motionKit.getGyroValues(interval: 1.0){

(x, y, z) in

//Do whatever you want with the x, y and z values

println("X: \(x) Y: \(y) Z \(z)")

}

Getting Magnetometer Data

Wanna feel the presence of a ghost inside your room by the Magnetic Field around your iDevice ? Well, it’ll take as much time as it takes to call a rescue service.