How to use iPhone’s accelerometer in the iPhone Simulator

Although this topic has been widely discussed on StackOverflow and Cocos2d-iPhone, there’s always a shortage of web results on the matter.

Hoping to change this and be of use to other iPhone developers around the web, here are the steps I followed to make this cool screencast of my motion sensor iPhone game, Mika’s Gem Run:



Tools of the trade:

First of all, get a screencast software.

I use ScreenFlow – the leader Mac software for screencasting. There’s also a free demo on their website, but since it adds a watermark over the resulting movie, it’s pretty useless for our purpose.

If you are short on money and want a free solution (although ScreenFlow is definitely worth $99), there are other alternatives: you can use the screencasting features built in Snow Leopard’s Quick Time player (although really basic), or use Camtasia – the product costs $99 also, but the free demo doesn’t add any watermark to the final screencast (but it does expire after a few uses of the software, so you’d better be prepared to use it).

Now that we have screencasting software, let’s get our accelerometer simulator:

  • Download http://code.google.com/p/accelerometer-simulator/ – it’s feature limited, but it’s free and will do the trick. There are also other, more expensive solutions, on the same basic idea, the most popular of them being iSimulate .
  • Open accelerometer-simulator in XCode, build and deploy to your development iPhone(naturally, after you’ve changed the certificates info in the Info.plist). Stop the execution, and open up your real project, the one you want to control on the simulator through the iPhone’s accelerometer.
    To embed Accelerometer Simulator capabilities into your own application, simply add the AccelerometerSimulation.h and AccelerometerSimulation.m files from the Simulator classes directory into your project.
    Then in the source file where you configure UIAccelerometer, simply add
    #import “AccelerometerSimulation.h”
    This will override the default behaviour of UIAccelerometer when run on the iPhone simulator. When building for device, nothing is changed in your application.
  • Build your project and run it in the simulator
  • with your iPhone still plugged in the Mac, start up the Accelerometer Simulator. In the Network tab you enable the network, then in the Address you type the IP of your Mac computer. Go back to the Accelerometer tab. Now, tilting the iPhone should reflect the changes to the iPhone Simulator as well

The last steps might seem more difficult then they actually are. The advantage of deploying the game to Simulator and controlling it from the real device is obvious though: testing the game is way faster, recompiling and deploying only take a few seconds instead of minutes; debugging is also easier. The most important thing, though, is that now we can make cute screencasts of our games.

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Leave a Reply