利用AddressBook.framework框架获取iOS系统通讯录数据
Posted Francis
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用AddressBook.framework框架获取iOS系统通讯录数据相关的知识,希望对你有一定的参考价值。
此方法是使用AddressBook.framework框架获取通讯录信息
第一步、在info.plist文件里面配置访问权限
第二步、导入头文件
1 #import <AddressBook/AddressBook.h> 2 #import <AddressBookUI/AddressBookUI.h>
第三步、获取通讯录数据
1 - (void)getSystemAddress{ 2 3 //新建一个通讯录类 4 ABAddressBookRef addressBooks = nil; 5 //判断是否在ios6.0版本以上 6 if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) 7 { 8 addressBooks = ABAddressBookCreateWithOptions(NULL, NULL); 9 //获取通讯录权限 10 dispatch_semaphore_t sema = dispatch_semaphore_create(0); 11 12 ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);}); 13 14 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); 15 } 16 else 17 { 18 CFErrorRef* error=nil; 19 addressBooks = ABAddressBookCreateWithOptions(NULL, error); 20 } 21 22 //获取通讯录中的所有人 23 CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks); 24 //通讯录中人数 25 CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks); 26 27 for (NSInteger i = 0; i < nPeople; i++) 28 { 29 ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i); 30 //获取个人名字 31 CFTypeRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty); 32 NSString *nameString = (__bridge NSString *)abName; 33 34 CFTypeRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty); 35 NSString *lastNameString = (__bridge NSString *)abLastName; 36 37 CFStringRef abFullName = ABRecordCopyCompositeName(person); 38 if ((__bridge id)abFullName != nil) 39 { 40 nameString = (__bridge NSString *)abFullName; 41 } 42 else 43 { 44 if ((__bridge id)abLastName != nil){ 45 nameString = [NSString stringWithFormat:@"%@ %@", nameString, lastNameString]; 46 } 47 } 48 //获取到的姓名 49 NSLog(@"name:%@", nameString); 50 51 ABPropertyID multiProperties[] = { 52 kABPersonPhoneProperty, 53 kABPersonEmailProperty 54 }; 55 NSInteger multiPropertiesTotal = sizeof(multiProperties) / sizeof(ABPropertyID); 56 for (NSInteger j = 0; j < multiPropertiesTotal; j++) 57 { 58 ABPropertyID property = multiProperties[j]; 59 ABMultiValueRef valuesRef = ABRecordCopyValue(person, property); 60 NSInteger valuesCount = 0; 61 if (valuesRef != nil){ 62 //获取每个联系人添加的电话号码数量 63 valuesCount = ABMultiValueGetCount(valuesRef); 64 } 65 if (valuesCount == 0) { 66 CFRelease(valuesRef); 67 continue; 68 } 69 //获取电话号码和email 70 for (NSInteger k = 0; k < valuesCount; k++) 71 { 72 CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k); 73 switch (j) 74 { 75 case 0: // Phone number 76 { 77 NSString * phoneNumber = (__bridge NSString*)value; 78 NSLog(@"电话:%@", phoneNumber); 79 break; 80 } 81 case 1: // Email 82 { 83 NSString *email = (__bridge NSString*)value; 84 NSLog(@"邮箱:%@", email); 85 break; 86 } 87 } 88 CFRelease(value); 89 } 90 } 91 } 92 }
第四步、获取到姓名、电话、邮箱后可建立model保存,这里只是打印出来
五、扩展、其他信息的获取方法
1 //获取当前联系人名字 2 NSString*firstName=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)); 3 //获取当前联系人姓氏 4 NSString*lastName=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty)); 5 //获取当前联系人中间名 6 NSString*middleName=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonMiddleNameProperty)); 7 //获取当前联系人的名字前缀 8 NSString*prefix=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonPrefixProperty)); 9 //获取当前联系人的名字后缀 10 NSString*suffix=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonSuffixProperty)); 11 //获取当前联系人的昵称 12 NSString*nickName=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonNicknameProperty)); 13 //获取当前联系人的名字拼音 14 NSString*firstNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty)); 15 //获取当前联系人的姓氏拼音 16 NSString*lastNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty)); 17 //获取当前联系人的中间名拼音 18 NSString*middleNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty)); 19 //获取当前联系人的公司 20 NSString*organization=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonOrganizationProperty)); 21 //获取当前联系人的职位 22 NSString*job=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonJobTitleProperty)); 23 //获取当前联系人的部门 24 NSString*department=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonDepartmentProperty)); 25 //获取当前联系人的生日 26 NSString*birthday=(__bridge NSDate*)(ABRecordCopyValue(person, kABPersonBirthdayProperty)); 27 NSMutableArray * emailArr = [[NSMutableArray alloc]init]; 28 //获取当前联系人的邮箱 注意是数组 29 ABMultiValueRef emails= ABRecordCopyValue(person, kABPersonEmailProperty); 30 for (NSInteger j=0; j<ABMultiValueGetCount(emails); j++) { 31 [emailArr addObject:(__bridge NSString *)(ABMultiValueCopyValueAtIndex(emails, j))]; 32 } 33 //获取当前联系人的备注 34 NSString*notes=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonNoteProperty)); 35 //获取创建当前联系人的时间 注意是NSDate 36 NSDate*creatTime=(__bridge NSDate*)(ABRecordCopyValue(person, kABPersonCreationDateProperty)); 37 //获取最近修改当前联系人的时间 38 NSDate*alterTime=(__bridge NSDate*)(ABRecordCopyValue(person, kABPersonModificationDateProperty)); 39 //获取地址 40 ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty); 41 for (int j=0; j<ABMultiValueGetCount(address); j++) { 42 //地址类型 43 NSString *type = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(address, j)); 44 NSDictionary * temDic = (__bridge NSDictionary *)(ABMultiValueCopyValueAtIndex(address, j)); 45 //地址字符串,可以按需求格式化 46 NSString * adress = [NSString stringWithFormat:@"国家:%@\\n省:%@\\n市:%@\\n街道:%@\\n邮编:%@",[temDic valueForKey:(NSString*)kABPersonAddressCountryKey],[temDic valueForKey:(NSString*)kABPersonAddressStateKey],[temDic valueForKey:(NSString*)kABPersonAddressCityKey],[temDic valueForKey:(NSString*)kABPersonAddressStreetKey],[temDic valueForKey:(NSString*)kABPersonAddressZIPKey]]; 47 } 48 //获取当前联系人头像图片 49 NSData*userImage=(__bridge NSData*)(ABPersonCopyImageData(person)); 50 //获取当前联系人纪念日 51 NSMutableArray * dateArr = [[NSMutableArray alloc]init]; 52 ABMultiValueRef dates= ABRecordCopyValue(person, kABPersonDateProperty); 53 for (NSInteger j=0; j<ABMultiValueGetCount(dates); j++) { 54 //获取纪念日日期 55 NSDate * data =(__bridge NSDate*)(ABMultiValueCopyValueAtIndex(dates, j)); 56 //获取纪念日名称 57 NSString * str =(__bridge NSString*)(ABMultiValueCopyLabelAtIndex(dates, j)); 58 NSDictionary * temDic = [NSDictionary dictionaryWithObject:data forKey:str]; 59 [dateArr addObject:temDic]; 60 }
以上是关于利用AddressBook.framework框架获取iOS系统通讯录数据的主要内容,如果未能解决你的问题,请参考以下文章