How to get contacts detail of iphone and make CSV file of that contact

后端 未结 4 1021
囚心锁ツ
囚心锁ツ 2020-12-14 12:56

I want to get contact details in an iPhone with information like First Name, Last Name, Phone Number, Phone Number Type, Email Address, Email Address Type etc..

Can

4条回答
  •  臣服心动
    2020-12-14 13:22

    I used iApple's code above as a starting point and created a working version from it - this one just collects all address book entries in an array. As mentioned above the original iApple doesn't work, there's a few bugs in it. This one works, and was tested.

    Note: This doesn't return any contacts that don't have a name set - you can remove that for your own code, I just did it because I only need contacts with names set, and NSMutableDictionary doesn't like nil entries (crashes).

    In my own address book I have a few entries that are just an email - I am not sure how they got there, but it's certainly possible to have address book entries without a name. Keep that in mind when iterating over an address book.

    I am using the full name as per Apple's recommendations - ABRecordCopyCompositeName returns a composite of first and last name in the order specified by the user.

    Finally, I made this a static method and put it in a helper class.

    This is for use with ARC!

    // returns an array of dictionaries
    // each dictionary has values: fullName, phoneNumbers, emails
    // fullName is a string
    // phoneNumbers is an array of strings
    // emails is an array of strings
    + (NSArray *)collectAddressBookContacts {
        NSMutableArray *allContacts = [[NSMutableArray alloc] init];
        ABAddressBookRef addressBook = ABAddressBookCreate();
        CFArrayRef people  = ABAddressBookCopyArrayOfAllPeople(addressBook);
        for(int i = 0;i

提交回复
热议问题