IOS开发- 访问通讯录,并将通讯录中姓名-头像-手机号码 发给服务器
现在很多软件都会访问通讯录,并将通讯录的信息取得,发给服务器,然后服务器会返回相应电话号码的用户是否注册。
现在分享一下前两步,访问通讯录并处理通讯录的信息
1、导入框架
#import <AddressBook/AddressBook.h>
2.拥有属性
{
ABAddressBookRef _book;
}
以及
//联系人数组 @property (nonatomic, strong) NSMutableArray *telphoneContacts;
3.在viewDidLoad中创建通讯录对象,并请求访问权限,并执行读取通讯录的方法
//创建通讯录对象 _book = ABAddressBookCreateWithOptions(NULL, NULL); //1.请求权限 CYAdressBookTableViewController *weakSelf = self; ABAddressBookRequestAccessWithCompletion(_book, ^(bool granted, CFErrorRef error) { // 用户允许应用访问通讯录数据 if (granted) { DLog(@"用户允许应用访问通讯录数据");
//读取通讯录
[weakSelf readContacts];
}else{ DLog(@"用户不允许应用访问通讯录数据"); [SVProgressHUD showInfoWithStatus:@"没有权限访问通讯录"]; } });
4.实现上一步中最后一行的读取通讯录方法。并将数组发送给服务器
///读取通讯录信息 - (void)readContacts { //创建一个临时数组,用于装手机号的md5值 NSMutableArray *phoneNumArray = [NSMutableArray array]; // 1.读取所有的联系人 CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(_book); // 2.遍历联系人 long count = CFArrayGetCount(allPeople); for (long i = 0; i < count; ++i) { // 一个联系人的数据 就是一条记录 ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i); // 获取姓 CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty); // 获取名 CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty); NSString *name = @""; if (firstName && lastName) { name = [NSString stringWithFormat:@"%@%@",lastName,firstName]; }else if(lastName && !firstName){ name = (__bridge NSString *)(lastName); }else if(!lastName && firstName){ name = (__bridge NSString *)(firstName); }else { name = @"(空)"; } //获取头像 NSData *imageData = (__bridge NSData*)ABPersonCopyImageData(person); UIImage *image = [UIImage imageWithData:imageData]; // 获取电话号码(多个) ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty); // 获取电话号码 long phonesCount = ABMultiValueGetCount(phones); for (long j = 0 ; j < phonesCount; ++j) { // 电话类型 CFStringRef phoneType = ABMultiValueCopyLabelAtIndex(phones, j); // 电话号码 CFStringRef phoneNum = ABMultiValueCopyValueAtIndex(phones, j); NSString *telphoneNum = (__bridge NSString *)phoneNum; //过滤电话号码中间的特殊字符 NSString *telphoneNum01 = [self stringByTrimmingCharactersInSet:telphoneNum];
if (telphoneNum01.length == 11) { //如果是11位的电话,MD5之后加入数组中 [phoneNumArray addObject:telphoneNum01.md5String]; //创建一个通讯录联系人对象(提前定义好的一个联系人类) UserInfo_TelphoneContact *contact = [[UserInfo_TelphoneContact alloc] init]; //设置电话号码 contact.telphoneNum = telphoneNum01; //设置电话号码的md5str contact.telphoneNumMd5Str = telphoneNum01.md5String; //联系人名字 contact.name = name; //头像 contact.image = image; //将对象加入电话联系人数组中 [self.telphoneContacts addObject:contact]; } CFRelease(phoneType); CFRelease(phoneNum); } CFRelease(phones); } //将电话数组发送给服务器 [self sendPhoneNumArray:phoneNumArray]; CFRelease(allPeople); }
5.实现第四步中的处理电话号码方法(因为iPhone的手机号码取出来是带空格或者-的)
///过滤字符串中间的特殊符号 - (NSString *)stringByTrimmingCharactersInSet:(NSString *)str { NSString *tempStr = @""; for (int i = 0; i < str.length; i++) { NSString *strc = [str substringWithRange:NSMakeRange(i, 1)]; if (![strc isEqual: @"-"] && ![strc isEqual: @"("]&& ![strc isEqual: @")"]&& ![strc isEqual: @" "]&& ![strc isEqual: @" "] ) { tempStr = [tempStr stringByAppendingString:strc]; } } return tempStr; }