首页 > 代码库 > 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 读取通讯录数据
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。