ABAddressBookCreateWithOptions causes crash (unrecognized selector)
In my iPad app (XCode 5, ios7, ARC, Storyboards), I'm trying to read the
Address book and get all of the entries. This is my code that I copied
from SO (why re-invent the wheel?). Unfortunataly, it doesn't work!
- (IBAction)importClientData:(UIButton *)sender {
NSMutableArray *allContacts = [[NSMutableArray alloc] init];
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
for(int i = 0;i<ABAddressBookGetPersonCount(addressBook);i++)
{
NSMutableDictionary *aPersonDict = [[NSMutableDictionary alloc] init];
ABRecordRef ref = CFArrayGetValueAtIndex(people, i);
NSString *fullName = (__bridge NSString *)
ABRecordCopyCompositeName(ref);
if (fullName) {
[aPersonDict setObject:fullName forKey:@"fullName"];
// collect phone numbers
NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
ABMultiValueRef phones = ABRecordCopyValue(ref,
kABPersonPhoneProperty);
for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++) {
NSString *phoneNumber = (__bridge NSString *)
ABMultiValueCopyValueAtIndex(phones, j);
[phoneNumbers addObject:phoneNumber];
}
[aPersonDict setObject:phoneNumbers forKey:@"phoneNumbers"];
// collect emails - key "emails" will contain an array of email
addresses
ABMultiValueRef emails = ABRecordCopyValue(ref,
kABPersonEmailProperty);
NSMutableArray *emailAddresses = [[NSMutableArray alloc] init];
for(CFIndex idx = 0; idx < ABMultiValueGetCount(emails); idx++) {
NSString *email = (__bridge NSString
*)ABMultiValueCopyValueAtIndex(emails, idx);
[emailAddresses addObject:email];
}
[aPersonDict setObject:emailAddresses forKey:@"emails"];
// if you want to collect any other info that's stored in the
address book, it follows the same pattern.
// you just need the right kABPerson.... property.
[allContacts addObject:aPersonDict];
}
else {
// Note: I have a few entries in my phone that don't have a name set
// Example one could have just an email address in their address
book.
}
}
}
No matter what code I use (I have tried several variations of the above
code) they all crash on the same call (-ABAddressBookCreateWithOptions).
Since I copied the same code from multiple examples, I would expect at
least this line to work. It's not! Why?
No comments:
Post a Comment