How to handle multiple properties on iPhone's addressbook?
The problem is that I always get runtime crashes at any method invoked at phones
variable.
At this version I get an error at 1
(ABMultiValueCopyValueAtIndex
).
If I'll comment this line, the code crashes at 2
(ABMultiValueGetCount
).
It looks like the property was empty. If I NSLog
the phones
variable. I get (null)
.
I test the code on iPhone Simulator, I have some dummy contacts there with some phone numbers.
Also firstName
and lastName
work like a charm.
for(id person in people){
NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (NSString *)AB开发者_StackOverflowRecordCopyValue(person, kABPersonLastNameProperty);
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString *phone = (NSString*)ABMultiValueCopyValueAtIndex(phones, 0); /*1*/
/*2/ id ph, phLb;
for (CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
phLb = ABMultiValueCopyLabelAtIndex(phones, i);
ph = ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"%@,%@", phLb, ph);
CFRelease(phLb);
CFRelease(ph);
}
*/
NSLog(@"%@", firstName);
NSLog(@"%@", lastName);
NSLog(@"%@", phones);
NSLog(@"%@", phone);
[firstName release];
[lastName release];
[phone release];
[phones release];
}
I think your assumptions about how to procure a ABMultiValueRef are wrong. I don't have my AB code handy at this computer, but verify that ABRecordCopyValue is the appropriate method. And then do research on ABMultiValueRef on how to access its contents. It's a whole different object set than simple strings.
The code:
addressBook = ABAddressBookCreate();
people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
CFRelease(addressBook);
deallocated the addressbook and all the objects that were reffered from it was released too. So the pointers to the MultiValueRefs were deallocated before use.
Learn to use a debugger. Also, your code leaks.
精彩评论