简体   繁体   中英

How to get someone's home phone number in address book on iOS?

I'm trying to find the home phone number of a contact in the address book. To do this, I'm comparing the label of the phone number with all the possible labels :

const ABPropertyID kABPersonPhoneProperty;
const CFStringRef kABPersonPhoneMobileLabel;
const CFStringRef kABPersonPhoneIPhoneLabel;
const CFStringRef kABPersonPhoneMainLabel;
const CFStringRef kABPersonPhoneHomeFAXLabel;
const CFStringRef kABPersonPhoneWorkFAXLabel;
const CFStringRef kABPersonPhoneOtherFAXLabel;
const CFStringRef kABPersonPhonePagerLabel;

First of all, the naming of these constants is strange - just by reading them, I can't tell which is the non-fax home phone label. Well, I'll just have to test all of them then and use the process of elimination.

NSString* phoneNumber = nil;

// record is an ABRecordRef
ABMultiValueRef phoneNumbers = ABRecordCopyValue(
    record,
    kABPersonPhoneProperty
);

if (ABMultiValueGetCount(phoneNumbers) > 0) {
    CFStringRef phoneLabelRef = ABMultiValueCopyLabelAtIndex(phoneNumbers, 0);
    NSString* phoneLabel = (__bridge_transfer NSString*)phoneLabelRef;

    NSLog(@"phone label: %@", phoneLabel);
    NSLog(@"phone is mobile: %d",  CFStringCompare(phoneLabelRef, kABPersonPhoneMobileLabel, 0) == kCFCompareEqualTo);
    NSLog(@"phone is main: %d",  CFStringCompare(phoneLabelRef, kABPersonPhoneMainLabel, 0) == kCFCompareEqualTo);
    NSLog(@"phone is iPhone: %d",  CFStringCompare(phoneLabelRef, kABPersonPhoneIPhoneLabel, 0) == kCFCompareEqualTo);
    NSLog(@"phone is home fax: %d",  CFStringCompare(phoneLabelRef, kABPersonPhoneHomeFAXLabel, 0) == kCFCompareEqualTo);
    NSLog(@"phone is work fax: %d",  CFStringCompare(phoneLabelRef, kABPersonPhoneWorkFAXLabel, 0) == kCFCompareEqualTo);
    NSLog(@"phone is other fax: %d",  CFStringCompare(phoneLabelRef, kABPersonPhoneOtherFAXLabel, 0) == kCFCompareEqualTo);
    NSLog(@"phone is pager: %d",  CFStringCompare(phoneLabelRef, kABPersonPhonePagerLabel, 0) == kCFCompareEqualTo);
}

Here are the printouts for two of the preloaded contacts in Simulator. Don't worry, all the contact information is fake.

phone label: _$!<Mobile>!$_
phone is mobile: 1
phone is main: 0
phone is iPhone: 0
phone is home fax: 0
phone is work fax: 0
phone is other fax: 0
phone is pager: 0
FIRST = Kate, LAST = Bell, PHONE = (555) 564-8583, EMAIL = kate-bell@mac.com

There's no problem with Kate Bell. I can detect that her phone is mobile. However, there's a problem with Anna Haro. Since none of the labels match, I can't figure out what type of phone number she has.

phone label: _$!<Home>!$_
phone is mobile: 0
phone is main: 0
phone is iPhone: 0
phone is home fax: 0
phone is work fax: 0
phone is other fax: 0
phone is pager: 0
FIRST = Anna, LAST = Haro, PHONE = 555-522-8243, EMAIL = anna-haro@mac.com

I'd prefer not to literally match the string _%!<Home>!$_ because the exact value could change with future versions of iOS.

You want the generic label kABHomeLabel . This is the "home" label used for phone numbers, emails, and addresses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM