最近开发的项目中有个获取用户手机通讯录部分信息的功能,了解了一下相关知识,这里做一下记录。
开始前先建一个存储手机通讯录的模型:
通讯录模型//查看是否已经获取通讯录权限
- (NSString *)loadLinkMan
{
__block NSString *resultStr;
ABAddressBookRef addressBookref = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookref, ^(bool granted, CFErrorRef error) {
CFErrorRef *error1 = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error1);
resultStr = [self accessAddressBook:addressBook];
});
}else if(ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
CFErrorRef *error1 = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error1);
resultStr = [self accessAddressBook:addressBook];
}else{
// UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没有获取通讯录权限" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
alert.delegate = self;
[alert show];
return nil;
}
return resultStr;
}
- (NSString *)accessAddressBook:(ABAddressBookRef)addressBook{
//获取联系人个数
CFIndex numberOfPeoples = ABAddressBookGetPersonCount(addressBook);
CFArrayRef peoples = ABAddressBookCopyArrayOfAllPeople(addressBook);
NSLog(@"有%ld个联系人", numberOfPeoples);
//循环获取联系人
for (int i = 0; i < numberOfPeoples; i++) {
ABRecordRef person = CFArrayGetValueAtIndex(peoples, i);
ABLinkManModel *linkMan = [[ABLinkManModel alloc] init];
linkMan.firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
linkMan.lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
linkMan.organization = (__bridge NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);
linkMan.name = [NSString stringWithFormat:@"%@%@",linkMan.lastName, linkMan.firstName];
if ([linkMan.name isEqualToString:@""]) {
linkMan.name = linkMan.organization; //name为空则取organization,为空返回""
}
//读取电话多值,可能有多个电话
NSMutableArray *phoneArray = [[NSMutableArray alloc] init];
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (int k = 0; k < ABMultiValueGetCount(phone); k++)
{
//获取电话别名
NSString * personPhoneLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));
//获取该别名下的电话值
NSString * tempstr = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phone, k);
NSArray *array = [NSArray arrayWithObjects:personPhoneLabel, tempstr, nil];
[phoneArray addObject:array];
}
linkMan.phones = phoneArray;
NSDictionary *dic = [linkMan mj_keyValues];
[self.linkManArray addObject:dic];
}
NSArray *arr = [self.linkManArray copy];
NSString *jsonStr;
if ([NSJSONSerialization isValidJSONObject:arr]) {
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:&error];
jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
return jsonStr; //后台需要通讯录json,返回json。
}
网友评论