Extending the iPhone Simulator

preserving old blog post from 2014-01-24 12:06:53 +0300

While working on the latest version of MyPermissions for iOS I found it extremely annoying that I couldnt access Xcodes Simulate Background fetch feature from the iPhone Simulator. Being a freelancer working on-site, without a desk of my own, I usually only have my laptop screen to work with, so going back and forth between Xcode and the Simulator was getting really frustrating.

After a few days of this I decided I had enough and took some time late at night to see if I could work out a solution. I decided to use F-Script to start analyzing the Simulator and see if I could find anything interesting. I injected F-Script into my running simulator but couldnt find anything super obvious using the object browser.

I remembered reading about a code injection technique on the Friday Q&A by Mike Ash, so following his example I made a small utility to inject into the simulator and register for all notifications. That certainly did the trick I found out about com.apple.iphonesimulator.sendApplicationEvent a notification sent by Xcode when simulating a background fetch. Cool.

I knew from my notification tracing tool, that the notification was being sent with the following userInfo dictionary:

    @"applicationEventType" : @"applicationEventBackgroundFetch",
    @"applicationPID" : @(appPID),
    @"operationUUID" : @(appPID)

but just to make sure, I opened Hopper Disassembler and searched for sendApplicationEvent reading the pseudocode produced by Hopper confirmed my findings.

Next on the agenda was adding a way to send the notification from the simulator. The simulator has no plugin framework like Xcode so I needed to inject my code from the outside. Ive started by simply using a script to load a framework using gdb. like so:

attach 'iPhone Simulator'
p (char)[[NSBundle bundleWithPath:@"~/BackgroundFetch.framework"] load]

But even when invoking this via an Automator service it was slow and annoying. My next obvious step was to create a SIMBL bundle, but SIMBL was known to be outdated and a root cause of many past problems. Lucky EasySIMBL seemed to be a modern lightweight alternative.

Thats how I ended up making SimAdditions which will hopefully become a set of additions and extensions to the iPhone Simulator.

My helpful screenshot

One upcoming feature Im currently working on is a themeable finger overlay (for demos and instruction videos), that as you can see is pretty broken right now :)