我已经编写了以下代码,用于从地址簿中提取联系人。
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
NSArray *people = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
for(id person in people){
//fetch multiple phone nos.
ABMultiValueRef multi = ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonPhoneProperty);
for (CFIndex j=0; j < ABMultiValueGetCount(multi); j++) {
NSString* phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multi, j);
[_devices addObject:phone];
}
}
但在 _devices 中,没有任何对象从手机添加。谁能让我知道我哪里错了。
答案 0 :(得分:0)
试试这段代码:
ABAddressBookRef address = ABAddressBookCreate( );
CFArrayRef People = ABAddressBookCopyArrayOfAllPeople( address );
CFIndex contact = ABAddressBookGetPersonCount( address );
for ( int j = 0; j < contact; j++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex( People, j );
}
答案 1 :(得分:0)
请尝试此代码
- (void)viewDidLoad
{
[super viewDidLoad];
phoneArray =[[NSMutableArray alloc]init];
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
{
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error)
{
if (granted)
{
[self getPhoneList];
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
{
[self getPhoneList];
}
}
-(void)getPhoneList
{
[phoneArray removeAllObjects];
ABAddressBookRef addressBook=ABAddressBookCreateWithOptions(NULL, NULL);
int count = (int)ABAddressBookGetPersonCount(addressBook);
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
for(int i = 0;i<count;i++)
{
ABRecordRef ref = CFArrayGetValueAtIndex(people, i);
ABMultiValueRef phones = ABRecordCopyValue(ref, kABPersonPhoneProperty);
for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
{
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);
NSString *phoneNumber = (NSString *)phoneNumberRef;
phoneNumber=[phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
phoneNumber=[phoneNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
phoneNumber=[phoneNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
phoneNumber=[phoneNumber stringByReplacingOccurrencesOfString:@") " withString:@""];
phoneNumber=[phoneNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
phoneNumber=[phoneNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
[phoneArray addObject:phoneNumber];
}
}