我正在创建一个应用程序,我必须在其中获取iPhone的联系人并使用此应用程序通过该应用程序进行调用。
CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);
for(int i = 0; i < numberOfPeople; i++)
{
ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
NSString *firstName = (NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
[TempArray addObject:[NSString stringWithFormat:@"%@",firstName]];
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++)
{
NSString *phoneNumber = (NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
[PhoneNumbers addObject:phoneNumber];
}
}
我得到的数字和名称都在不同的数组中。但是有一些联系人姓名包含多个号码,因此Number Number中的组件更多。
有什么能够同时将数字和联系人同步吗? 有没有什么可以扫描该列表中的手机号码和固定电话号码?
同样,在提取联系人时,我们可以一次获取朋友的完整名称,而不使用kABPersonFirstNameProperty或kABPersonLastNameProperty并附加?
任何建议?
答案 0 :(得分:1)
我不得不花一些时间来完成这项工作。我必须使用 ABMultiValueCopyLabelAtIndex 来获得这种联系,并与kABPersonPhoneMobileLabel
和kABPersonPhoneIPhoneLabel
进行比较,在字典表单中获取联系人详细信息。在此表单中,数字与名称同步。
contactList=[[NSMutableArray alloc] init];
ABAddressBookRef m_addressbook = ABAddressBookCreate();
if (!m_addressbook) {
NSLog(@"opening address book");
}
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
for (int i=0;i < nPeople;i++) {
NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
//For username and surname
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty);
CFStringRef firstName, lastName;
firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
[dOfPerson setObject:[NSString stringWithFormat:@"%@ %@", firstName, lastName] forKey:@"name"];
//For Email ids
ABMutableMultiValueRef eMail = ABRecordCopyValue(ref, kABPersonEmailProperty);
if(ABMultiValueGetCount(eMail) > 0) {
[dOfPerson setObject:(NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:@"email"];
}
//For Phone number
NSString* mobileLabel;
for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
{
[dOfPerson setObject:(NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
}
else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
{
[dOfPerson setObject:(NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
break ;
}
[contactList addObject:dOfPerson];
}
NSLog(@"array is %@",contactList);
}