IOKit is one of iOS’s private frameworks, and is used to access hardware and device capabilities.
WARNING: since this is a private framework, if you submit an app to the App Store that links to this framework, it will be rejected.
That said, let’s start by creating a new Xcode Project. You can pick any of the predefined templates.
Once that is done, access the Project Settings, then select the Target. Go to Build Phases, then the Link Binary With Libraries section.
Press the + sign to link a library. From the list select libIOKit.dylib. This is the IOKit framework.
Ok, so now we have a project linked to IOKit, we can finally use it to do all kinds of neat things!
Not so fast. First we need the headers for IOKit, so we can tell what to import.
Download the zip of the project (or clone it if you know your way around git), then place the header files in a folder that you might use for other projects. I placed it in /Users/MyUser/Code/Headers.
We must now tell Xcode where to find these files.
Go to your target’s Build Settings. In the search bar write “headers”. In the results you’ll see a section called Search Paths. Change “Always Search User Paths” to YES, and add the path where you placed your headers in “User Header Search Paths”. Make sure to put the folder that contains the IOKit folder, and not the IOKit folder itself.
IOKit imports a file not present in these headers, head here to obtain it. Place it in a folder called libkern, inside the Headers folder that you created.
Now that everything’s in place, where is some actual IOKit code?
In this wiki we see an example that uses IOKit, suggested by one of iOS blog’s readers.
This example attempts to detect the device’s Ambient Light Sensor events and log them.
While it’s possible to compile it within the project we setup, running it on either an iPhone or iPad will crash on this line:
CFArrayRef srvs =
(CFArrayRef)IOHIDEventSystemCopyMatchingServices(system, dict, 0, 0, 0,0);
The IOHIDEventSystemCopyMatchingServices function fails and doesn’t return a workable array. Perhaps one of our readers can help?
Grab the UIDevice-IOKitExtensions.h and UIDevice-IOKitExtensions.m files, copy them into your project and import the header file.
Now on your ViewController you can simply call:
[[UIDevice currentDevice] imei];
[[UIDevice currentDevice] backlightlevel];
These will get your device’s IMEI and backlight level. Not as exciting as handling your device’s light sensor events, but still interesting.