如何从地址簿中获取姓名,电子邮件ID和所有电话号码并在我的ios应用程序中显示?

时间:2014-03-31 06:32:53

标签: ios addressbook

我正在开发一个应用程序,其中我想从地址簿中获取所有联系人并在我的应用程序中显示。我已获得所有数据但问题是,l此数据不是按字母顺序升序,也是电话号码,电子邮件ID应按照名称的正确顺序排列。我将所有数据放在不同的不同数组中。请给出一些正确的想法。

2 个答案:

答案 0 :(得分:4)

首先在您的.m文件中导入<AddressBook/AddressBook.h>

ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
    NSMutableArray *allEmails = [[NSMutableArray alloc] initWithCapacity:CFArrayGetCount(people)];
    for (CFIndex i = 0; i < CFArrayGetCount(people); i++) {
        ABRecordRef person = CFArrayGetValueAtIndex(people, i);
        ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
        for (CFIndex j=0; j < ABMultiValueGetCount(emails); j++) {
            NSString* email = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emails, j);
            [allEmails addObject:email];

        }
        CFRelease(emails);
    }
    NSLog(@"All Detils:%@",allEmails);
    CFRelease(addressBook);
    CFRelease(people);

你可以按照上面的代码获得所有的电子邮件地址。你想知道名字然后改变。

ABMultiValueRef Name = ABRecordCopyValue(person, kABPersonFirstNameProperty);

联系电话:

ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

只有您更改了所需的人物属性。

// Property keys
AB_EXTERN const ABPropertyID kABPersonFirstNameProperty;          // First name - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonLastNameProperty;           // Last name - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonMiddleNameProperty;         // Middle name - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonPrefixProperty;             // Prefix ("Sir" "Duke" "General") - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonSuffixProperty;             // Suffix ("Jr." "Sr." "III") - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonNicknameProperty;           // Nickname - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonFirstNamePhoneticProperty;  // First name Phonetic - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonLastNamePhoneticProperty;   // Last name Phonetic - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonMiddleNamePhoneticProperty; // Middle name Phonetic - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonOrganizationProperty;       // Company name - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonJobTitleProperty;           // Job Title - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonDepartmentProperty;         // Department name - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonEmailProperty;              // Email(s) - kABMultiStringPropertyType
AB_EXTERN const ABPropertyID kABPersonBirthdayProperty;           // Birthday associated with this person - kABDateTimePropertyType
AB_EXTERN const ABPropertyID kABPersonNoteProperty;               // Note - kABStringPropertyType
AB_EXTERN const ABPropertyID kABPersonCreationDateProperty;       // Creation Date (when first saved)
AB_EXTERN const ABPropertyID kABPersonModificationDateProperty;   // Last saved date

按字母顺序对该数组进行排序后

NSArray *EmailArray = [allEmails sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

答案 1 :(得分:0)

如果您已成功找到联系人并将其存储在数组中,则可以使用localizedCaseInsensitiveCompare:

按字母顺序对其进行排序
NSArray *sortedArray = [YOUR_ARRAY sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

只需浏览Apple Docs,即可找到所有内容。