[英]How to get all phone numbers with labels for all contacts in address book?
I want to get all the phone numbers with diff labels like "iPhone", "home phone", "mobile number", "other numbers" , etc. for a contact stored in iPhone address book. 我想获取所有带有diff标签的电话号码,例如“ iPhone”,“家用电话”,“手机号码”,“其他号码”等,用于存储在iPhone通讯录中的联系人。
How do I get it? 我如何得到它?
Please help. 请帮忙。
Thanks in advance. 提前致谢。
I am trying: which is crashing 我正在尝试:崩溃了
ABAddressBookRef ab=ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(ab);
NSMutableArray *allNumbers = [[NSMutableArray alloc] initWithCapacity:CFArrayGetCount(people)];
for (CFIndex i = 0; i < CFArrayGetCount(people); i++) {
ABRecordRef person = CFArrayGetValueAtIndex(people, i);
ABMultiValueRef numbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (CFIndex j=0; j < ABMultiValueGetCount(numbers); j++) {
CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(numbers, i);
NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(numbers, j);
CFStringRef locLabel1 = ABMultiValueCopyLabelAtIndex(numbers, j);
NSString *phoneLabel1 =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);
NSLog(@" ####### phone no -> %@ , phone label -> %@ #######)", locLabel1, phoneLabel1);
//CFRelease(phones);
NSString *phoneNumber = (NSString *)phoneNumberRef;
CFRelease(phoneNumberRef);
CFRelease(locLabel);
NSLog(@"phone no -> %@ , phone label -> %@)", phoneNumber, phoneLabel);
[phoneNumber release];
}
CFRelease(numbers);
}
CFRelease(people);
Try: 尝试:
ABMultiValueRef *phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);
for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
{
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);
CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(phones, j);
NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);
NSString *phoneNumber = (NSString *)phoneNumberRef;
CFRelease(phoneNumberRef);
CFRelease(locLabel);
NSLog(@" - %@ (%@)", phoneNumber, phoneLabel);
[phoneNumber release];
}
This is solid for ARC 64bit iOS8: 这对于ARC 64位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;
}
Solved it this way: 这样解决:
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef all = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex n = ABAddressBookGetPersonCount(addressBook);
for( int i = 0 ; i < n ; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex(all, i);
NSString *firstName = (NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSLog(@"Name %@", firstName);
ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);
for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
{
CFStringRef locLabel1 = ABMultiValueCopyLabelAtIndex(phones, j);
NSString *phoneLabel1 =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel1);
NSLog(@" ### %@ --- %@ ### )", locLabel1, phoneLabel1);
}
}
Also to get phone number with particular record id, do this: 同样要获取具有特定记录ID的电话号码,请执行以下操作:
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef all = ABAddressBookCopyArrayOfAllPeople(addressBook);
ABRecordRef ref = CFArrayGetValueAtIndex(all, indexPath.row);
NSString *firstName = (NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSLog(@"Name %@", firstName);
NSInteger myID = ABRecordGetRecordID(ref);
NSLog(@"Record id is > %d", myID);
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook,myID);
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
{
NSString* num = (NSString*)ABMultiValueCopyValueAtIndex(phones, j);
CFStringRef locLabel1 = ABMultiValueCopyLabelAtIndex(phones, j);
NSString *phoneLabel1 =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel1);
NSLog(@"%@ --- %@ )", num, phoneLabel1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.