iOS Simulator photos and addressbook permissions

05 Jun 2014

On an iOS device you don’t want all of your apps just having access to your photos and addressbook contacts. Therefor Apple has a system in place, that asks you to give access to these resources when an app tries to access them for the first time. Great feature!

Not so great when you want to write tests for your own app and the classes that you want to test trigger exactly this behaviour. Your tests will be stuck on the UIAlertView dialog, asking the user to grant access. Unfortunately the current user is your automatic test case, so nobody will ever tap this button on the dialog.

So far Apple has not provided any solution to this problem. So I did some reverse engineering on the iOS Simulator to see where it stores this kind of information: Which App does have access to the photos library or the addressbook. Turns out there is some SQLite database file. Great, let’s modify that! Normally an iOS application is sandboxed and cannot access any data outside its sandbox. This is not true for the iOS Simulator though. So we can just add the current Bundle Identifier to this database and we’re all set.

Even easier: I’ve wrapped up some library to do that. You can install it via CocoaPods:

target 'YourAppTests' do
  pod 'JPSimulatorHacks'
end

And in your test case:

#import <JPSimulatorHacks/JPSimulatorHacks.h>

@implementation MyAppTests

+ (void)setUp
{
    [super setUp];
    [JPSimulatorHacks grantAccessToAddressBook];
    [JPSimulatorHacks grantAccessToPhotos];
}

@end

The source code is available on GitHub: plu/JPSimulatorHacks