How to fetch name, email id and all phone numbers from addressbook and show in my ios app? [duplicate]

心不动则不痛 提交于 2019-12-25 19:04:26

问题


I am developing an app, in which I want to fetch all the contacts from addressbook and show in my app.I have get all the data but problem is,l this data is not in ascending order alphabetically, also phone numbers, email id should be arrange in proper sequence as names.I have all the data in different different arrays.Please give some idea for correct this.


回答1:


First of all import <AddressBook/AddressBook.h> in Your .m File

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);

You Can get all emailAdress as above code.You wnat to First name then change.

ABMultiValueRef Name = ABRecordCopyValue(person, kABPersonFirstNameProperty);

Contact number:

ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

Only You changes person property which you want.

// 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

After sort that array in alphabetical order

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



回答2:


If you have successfully find out the contacts and stored them in array, then you can sort it alphabetically using localizedCaseInsensitiveCompare:

NSArray *sortedArray = [YOUR_ARRAY sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

Just go through Apple Docs, you can find everything there.



来源:https://stackoverflow.com/questions/22754921/how-to-fetch-name-email-id-and-all-phone-numbers-from-addressbook-and-show-in-m

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!