首页 > 代码库 > ios 读取通讯录数据

ios 读取通讯录数据

#import <Foundation/Foundation.h>@interface LoadingContactData : NSObject// 读取通讯录+ (LoadingContactData *)shareIntance;- (void)getContactData;@end#import "LoadingContactData.h"static LoadingContactData *_contactData;@implementation LoadingContactData+ (LoadingContactData *)shareIntance{    if (_contactData =http://www.mamicode.com/= nil) {        _contactData = [[LoadingContactData alloc]init];    }    return _contactData;}- (void)getContactData{    CFErrorRef error = NULL;        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);    if (addressBook) {        NSArray *allContacts = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);       NSMutableArray *contactsArray = [NSMutableArray new];                for (NSUInteger i = 0; i<[allContacts count]; i++)        {            ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];                        // Get mobile number            ABMultiValueRef phonesRef = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);            NSMutableArray *telephoneArray = [self getMobilePhoneProperty:phonesRef];                        [telephoneArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {                                THContact *contact = [[THContact alloc] init];                                // Get first and last names and fullName                NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);                NSString *lastName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);                NSString *fullName = (__bridge_transfer NSString *)ABRecordCopyCompositeName(contactPerson);                                // Set Contact properties                contact.firstName = firstName;                contact.lastName = lastName;                                if (fullName != nil){                    contact.fullName = fullName;                }                else if(firstName != nil && lastName != nil) {                    contact.fullName = [NSString stringWithFormat:@"%@%@",lastName,firstName];                } else if (firstName != nil) {                    contact.fullName = firstName;                } else if (lastName != nil) {                    contact.fullName = lastName;                } else {                    contact.fullName = @"";                }                                // Get image if it exists                NSData * imgData = http://www.mamicode.com/(__bridge_transfer NSData *)ABPersonCopyImageDataWithFormat(contactPerson,kABPersonImageFormatThumbnail); // 联系人头像(裁剪后的)                //NSData  *imgData = http://www.mamicode.com/(__bridge_transfer NSData *)ABPersonCopyImageData(contactPerson);          // 联系人头像原图                UIImage *userImg = [UIImage imageWithData:imgData];                contact.image = userImg;                                if (!contact.image) {                    contact.image = MF_PngWithSkin(DEFAULTS_SKIN, @"icon-avatar.png");                }                contact.phone = [obj telephoneWithReformat];                                if ([Unity validateMobile:contact.phone]) {                    contact.recordId = ABRecordGetRecordID(contactPerson)+ [[contact.phone substringWithRange:NSMakeRange(2, 8)] integerValue]+idx;                    [contactsArray addObject:contact];                }            }];                if(phonesRef) {                CFRelease(phonesRef);            }                    }                [[ContactDataArray shareContactData] setContactDataArray:contactsArray];                if(addressBook) {            CFRelease(addressBook);        }    }    else    {        NSLog(@"Error");            }}- (NSMutableArray *)getMobilePhoneProperty:(ABMultiValueRef)phonesRef{    NSMutableArray *dataArray = [[NSMutableArray alloc]init];    for (int i=0; i < ABMultiValueGetCount(phonesRef); i++) {            CFStringRef currentPhoneValue = ABMultiValueCopyValueAtIndex(phonesRef, i);         NSString *phone = (__bridge NSString *)currentPhoneValue;        [dataArray addObject:phone];    }        return dataArray;}@end

 

ios 读取通讯录数据