Add fake Contacts to the iOS address book

Handy method to add lots of fake contacts to an iOS AddessBook. Be careful you don’t run this on you real device!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
-(void) addFakeContacts{
ABAddressBookRef addressBook = ABAddressBookCreate();
// create 200 random contacts
for (int i = 201; i < 400; i++)
{
// create an ABRecordRef
ABRecordRef record = ABPersonCreate();
ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABStringPropertyType);
NSString *email = [NSString stringWithFormat:@"%i@%ifoo.com", i, i];
ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)(email), kABHomeLabel, NULL);
NSString *fname = [NSString stringWithFormat:@"Name %i", i];
NSString *lname = [NSString stringWithFormat:@"Last %i", i];
// add the first name
ABRecordSetValue(record, kABPersonFirstNameProperty, (__bridge CFTypeRef)(fname), NULL);
// add the last name
ABRecordSetValue(record, kABPersonLastNameProperty, (__bridge CFTypeRef)(lname), NULL);
// add the home email
ABRecordSetValue(record, kABPersonEmailProperty, multi, NULL);
// add the record
ABAddressBookAddRecord(addressBook, record, NULL);
}
// save the address book
ABAddressBookSave(addressBook, NULL);
// release
CFRelease(addressBook);
}

Use this to add a phone number to the ‘home’ element

1
2
3
NSString *phone = [NSString stringWithFormat:@"206427%i", i];
ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)(phone), kABHomeLabel, NULL);
ABRecordSetValue(record, kABPersonPhoneProperty, multi, NULL);

via: http://www.normyee.net/