iPhone accelerometer use in the simulator
May 30th, 2009 by Bill
One of the areas where the iPhone SDK and the simulator fall short becomes obvious when trying to develop and test applications that use the accelerometer. Invariably developers find themselves deploying to the actual device to do any sort of real testing. Well my friends, thanks to an ingenious little application called the Accelerometer Simulator, or AccSim, written by otto.chrons at Google Code, you no longer have to deploy your application to your device in order to test.
Now, you still have to have a compatible device connected to your Mac or network, however it controls the accelerometer in the sim. In this article, we are going to go step by step through the integration of this application and how to use it against the accelerometer graph sample provided by Apple.
First of all, we need to do a few things and we need to go get a few resources.
Requirement 1: You must be signed up for a paid Apple developer account
This will only work for individuals who have a paid Apple developer account. Yeah, I know, this stinks, however Apples policy is to prevent anyone from deploying their applications to their devices until they are a paid Apple developer with a properly provisioned device. If you haven’t read my article on Getting started in iPhone / iPod touch development you may want to do that now.
Requirement 2: Use svn to download AccSim from:
http://code.google.com/p/accelerometer-simulator/source/checkout
Requirement 3: Go get Apple’s accelerometer source code from:
http://developer.apple.com/iphone/library/samplecode/AccelerometerGraph/index.html
Now the fun begins, first open up the AccSim xCode project, set it deploy to the device (I use Device - iPhone OS 2.1). I did not encounter any build or deployment errors. If you do get something about code signing, you’ll have to fix that in your project settings or by editing your Info.plist file (check out the Bundle identifier). Close this project and run the simulator on your device.
When running on your device, it should look like this:

Switch to the Network view and turn the network ON.

Now, open the the Accelerometer graph sample application in xCode, and drag the following two files from the “Simulator classes” directory in the AccSim project into the Accelerometer graph project:
AccelerometerSimulation.m
After you’ve added the files to the project, open the file AppDelegate.m and add the following import statement (I placed it after the other imports).
Here is a picture of what this should look like (click on the picture for a full sized version, it’s a little hard to read):

Now, compile and run your Accelerometer graph application and it should pick up the motion of your device. If it isn’t working, verify that your device has not gone to sleep and that the network is enabled in the Network view. If you still have trouble, try changing the UDP port in the AccelerometerSimulation.m file, look for the line:
Make sure to make the corresponding change in the Network view on your device as well. Here’s a short video of the AccSim application controlling the xCode simulator:
When it is time to deploy your application, just remove the #import that you added and you’re ready to go. I hope you enjoyed this tutorial.
