Show Menu

Need an iOS Developer?

Submit your 30 day Job Listing for FREE

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.

At this stage, you would normally have to use tools like class-dump or otx to obtain the headers from the framework. However, someone has already done the job, and you can get the IOKit headers here.

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?

Since I don’t want to leave you hanging without an IOKit example, look here for Erica Sadun’s UIDevice extensions, one of which uses IOKit.

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.

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly


Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.


Written by:

Comments

There are currently: 10 Responses to “IOKit: An Introduction”. Leave your comment!

Might be a dumb question, but does using private APIs require jailbreaking a device?

What I am trying to do is to drive some automation into the native bits of the MobileSafari.app. One way would be to inject keyboard/touch events and I am seeing if I can use IOKit to do that. The automation is only for testing purposes, so it will not need to be submitted into the app store.


IOHIDEventSystemCopyMatchingServices is not the culprit. It seems that IOHIDEventSystemCreate returns a null pointer, and it gets passed to the function that crashes.


Can you please also post a simple guide about creating a daemon in iphone? Thanks


Hi there, great walkthrough to getting started with IOKit. Any updates RE: IOHIDEventSystemCopyMatchingServices returning an invalid CFArray?

I notice that there are two overrides for IOHIDEventSystemCopyMatchingServices, so perhaps its worth investigating using the other override?


Hiya,

Again, my thanks for putting all of this into context.

Question:

am using Xcode 4.2 targeting iOS5.
have followed your instructions including Erica’s downloads etc.
However … am getting following error
Error:
ld: library not found for -lIOKit
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/clang failed with exit code 1

notes; whilst it can be ‘seen’ within Xcode Build Phase BUT having looked into the privateframworks directory itself I cannot find IOKIT?

any ideas how to proceed?

best regards, Paul.


    Are you building and testing it in the iPhone Simulator? The private framework IOKit built for the Simulator doesn’t exist, so it won’t work there.


“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.”

What’s the point of using it then?

Thanks


Yet another superb post. Is this a new author?

I love the writing style :) – now.. off to enter a competition :)


Thanks for putting up the tutorial. Really helps esp because there is little information elsewhere on how to add or use the framework.


Leave a Reply

Would you like to sign up to the mailing list by our sister site: SwiftMonthly?