Questions
Questions

Reputation: 20925

How to retrieve mobile numbers from iPhone Contacts.

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.

Upvotes: 5

Views: 7353

Answers (4)

Andrew Bennett
Andrew Bennett

Reputation: 940

This is solid for ARC 64bit iOS8:

- (NSArray *)phoneNumbersOfContactAsStrings:(ABRecordRef)contactRef {

NSMutableArray *mobilePhones = [NSMutableArray arrayWithCapacity:0];

ABMultiValueRef phones = ABRecordCopyValue(contactRef, kABPersonPhoneProperty);
NSArray *allPhoneNumbers = (NSArray *)CFBridgingRelease(ABMultiValueCopyArrayOfAllValues(phones));

for (NSUInteger i=0; i < [allPhoneNumbers count]; i++) {
    if ([(NSString *)CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, (long)i)) isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) {
        [mobilePhones addObject:CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, (long)i))];
    }
    if ([(NSString *)CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, (long)i)) isEqualToString:(NSString *)kABPersonPhoneIPhoneLabel]) {
        [mobilePhones addObject:CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, (long)i))];
    }
}

CFRelease(phones);
return mobilePhones;
}

Upvotes: 0

kzw
kzw

Reputation: 161

check the ABPerson Refrence and You need't use @"$!!$" but kABPersonPhoneMobileLabel. the example is:

    ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString* mobile=@"";
    NSString* mobileLabel;
    for (int i=0; i < ABMultiValueGetCount(phones); i++) {
        //NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);
        //NSLog(@"%@", phone);
        mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
        if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel]) {
            NSLog(@"mobile:");
        } else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) {
            NSLog(@"iphone:");
        } else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhonePagerLabel]) {
            NSLog(@"pager:");
        }
        [mobile release];
        mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
        NSLog(@"%@", mobile);
    }

Upvotes: 16

Questions
Questions

Reputation: 20925

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;
    }
}

Upvotes: 2

Stelian Iancu
Stelian Iancu

Reputation: 2492

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;
    }
}

Upvotes: 5

Related Questions