如何在iOS中获取联系人?

时间:2015-09-12 07:13:00

标签: ios objective-c abaddressbook

我正在进行演示,以便将选定的单个联系人添加到我的应用中。我在github上看到有很多库,但所有这些库都基于podfile。但我不想podfile。那么我怎样才能得到解决方案呢?

1 个答案:

答案 0 :(得分:1)

检查此代码。

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 = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
    NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
    NSLog(@"Name:%@ %@", firstName, lastName);

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

    for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
        NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
        NSLog(@"Phone:%@", phoneNumber);
    }

}

确保将地址簿框架添加到项目中