views:

37

answers:

2

Hi,everyone,

I use the following code to set the retrieve the phone number in my app.

CFStringRef addressBookMobile;
ABRecordRef person;
NSString *mobile;

person = CFArrayGetValueAtIndex(people, i);
addressBookMobile = ABRecordCopyValue(person, kABPersonPhoneProperty);
mobile = [NSString stringWithFormat:@"%@", addressBookMobile];

The tag of the contacts is 'mobile'. However, when I use the NSLog(@"%@", mobile); . It displays the <NSCFType: 0x802ffc0>. does any problem for my code?

Should I use the const CFStringRef kABPersonPhoneMobileLabel and how to use? As if I replace it as the above code, it has the error. Can anyone help me? Thank you.

+1  A: 

The phone numbers of a person in the Address Book are in the form of a multi-value property.

In your case, you should have something like the following (haven't tried it, typing directly here, so I don't know if it compiles and/or works):

ABMultiValueRef phoneNumbers = (NSString *)ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString *mobileNumber;
NSString *mobileLabel;
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
    mobileLabel = (NSString *)ABMultiValueCopyLabelAtIndex(mobilePhones, i);
    if ([mobileLabel isEqualToString:@"mobile"]) {
        mobileNumber = (NSString*)ABMultiValueCopyValueAtIndex(mobilePhones,i);
        break;
    }
}
Stelian Iancu
@Stelian lancu, thank you for your reply. But I find that the solution may incorrect. the result is the same and I want to ask, what is the mobilePhones in the code? Thank you.
Questions
Sorry Mark, it should have been phoneNumbers :-). As I said, I typed it directly in the form, not in Xcode :-). So instead of mobilePhones, replace with phoneNumbers and it should be all good.
Stelian Iancu
A: 
ABMultiValueRef phoneNumbers = (NSString *)ABRecordCopyValue(person, kABPersonPhoneProperty);
CRStringRef mobileNumber;
CRStringRef mobileLabel;
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
    mobileLabel = ABMultiValueCopyLabelAtIndex(phoneNumbers, i);
    if ([mobileLabel isEqualToString:@"_$!<Mobile>!$_"]) {
        mobileNumber = ABMultiValueCopyValueAtIndex(phoneNumbers,i);
        break;
    }
}
Questions