我使用以下代码将联系人添加到我们的应用程序中。
此代码在设备 allPeople 获取零件的模拟器中正常工作
我忘记了任何事情,请帮助我
if(addressBookCollection==nil) {
addressBookCollection = ABAddressBookCreate();
}
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBookCollection);
CFArrayRef allPeople= ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering (addressBookCollection,source,1);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBookCollection);
NSMutableArray* tempContactArray = [[NSMutableArray alloc] initWithCapacity:nPeople];
for(int i=0 ; i < nPeople; i++)
{
......
}
答案 0 :(得分:2)
#import<AddressBook/AddressBook.h>
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef all = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex n = ABAddressBookGetPersonCount(addressBook);
for( int i = 0 ; i < n ; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex(all, i);
ABMultiValueRef contactnumber = ABRecordCopyValue(ref, kABPersonPhoneProperty);
for(CFIndex j = 0; j < ABMultiValueGetCount(contactnumber); j++)
{
CFStringRef contactnumberRef = ABMultiValueCopyValueAtIndex(contactnumber, j);
NSString *contactnumberstr = (NSString *)contactnumberRef;
CFRelease(contactnumberRef); [self.contactnumberArray contactnumber];
[contactnumber release];
}
}
您将获得“self.contactnumberArray”数组中的联系人。
享受!