How to make random entries into the iPhone address book

Are you a frustrated iOS developer working on an app that has to access the built-in address book on the device? Frustrated because you can’t find sample data to populate the address book in your Simulator, and you are too busy to create a bunch of test data?

Well, for a limited time* only, we at the Do Something Here Labs have a deal for you. For just a minimal** postage and handling charge, you too can use this sample code below to create random entries to your hearts content in your very own iOS app.

Here is the code in question. Simply drop this method into your Objective-C source code and call it in your app, and you will soon have contacts, contacts, and more contacts.

#import <AddressBook/AddressBook.h>
- (void)addressBookData
#define GENDER_OF_NAMES             @"m"    // use m for male or f for female
#define URL_FORMAT  @""
    NSString *urlString = [NSString stringWithFormat:URL_FORMAT, GENDER_OF_NAMES, NUMBER_OF_RANDOM_ENTRIES];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
    NSString *stringData = [NSString stringWithUTF8String:[data bytes]];
    NSString *areaCode = [NSString stringWithFormat:@"%03d", (arc4random() % 799 + 200)];
    NSArray *lineArray = [stringData componentsSeparatedByString:@"\n"];
    for (NSString *line in lineArray)
        if ([line length] > 0)
            NSArray *fieldArray = [line componentsSeparatedByString:@","];
            if ([fieldArray count] == 3)
                ABAddressBookRef addressBook = ABAddressBookCreate(); 
                ABRecordRef person = ABPersonCreate();
                NSString *phone = [NSString stringWithFormat:@"%@-%03d-%04d", areaCode,
                                   (arc4random() % 799 + 200), (arc4random() % 9999)];   
                ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);
                CFStringRef phoneType = (arc4random() % 2 == 0 ? kABPersonPhoneMainLabel : kABPersonPhoneMobileLabel);
                ABMultiValueAddValueAndLabel(phoneNumberMultiValue, phone, phoneType, NULL);
                ABRecordSetValue(person, kABPersonFirstNameProperty, [fieldArray objectAtIndex:1], nil);
                ABRecordSetValue(person, kABPersonLastNameProperty, [fieldArray objectAtIndex:2], nil);
                ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
                ABAddressBookAddRecord(addressBook, person, nil);
                ABAddressBookSave(addressBook, nil);
                NSLog(@"Created record for %@ %@ (%@: %@)", [fieldArray objectAtIndex:1], 
                      [fieldArray objectAtIndex:2], phoneType, phone);
    NSLog(@"Done creating address book data.");

On a housekeeping note, make sure to include the AddressBook.framework library in your application, and make sure to import the AddressBook.h header file as shown. Although if you are already developing an app that accesses the address book, you probably have this covered.

Also, your device or computer needs to have internet access while the code is running, as the code accesses a simple PHP application that I did a while back to do random sports roster generation. (The PHP application will generate a maximum of 99 entries at a time, so if you want to try and have it generate 42 billion names with one call, go right on ahead and try.) OK, I probably should not be blocking the main thread with the synchronous request, but I did not have time do something other than the most quick and dirty solution possible.

But wait, there’s more.

Happy birthday to Terry Bradshaw. I’m sure that if Terry was developing an iOS app, and he needed to populate some random address book entries, this would be the code he would use. And if it is good enough for Terry, isn’t it good enough for everyone?

(* = For as long as this web site is accessible on the internet)

(** = $0.00)

Leave a Reply