因为项目中需要调用系统的通讯录,所以对这方面做了一些研究,在调研的时候感觉调用通讯录这块资料并不是很多,所以就想和大家分享一下我调用系统通讯录的一些代码
思路:1.首先要获取系统的权限 (苹果为保护用户信息安全的设置,需获得用户的授权许可)
2.利用系统提供的api来调用
3.实现通讯录代理方法
建议:建议使用真机调试(在我的demo中使用了正则,屏蔽掉了座机等通讯录存储信息,只取手机号码)
// ViewController.m
// GetSystemAddressBook
//
// Created by 张一力 on 15/10/19.
// Copyright © 2015年 张一力. All rights reserved.
//
//ios 8.0之前使用;
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
CGFloat systemVersion = [[UIDevice currentDevice].systemVersion floatValue];
//联系人name
NSString * name = (__bridge NSString *)ABRecordCopyCompositeName(person);
userName = name;
NSLog(@"取出姓名%@ 系统%f",name,systemVersion);
nameLabel.text = name;
//判断点击区域
if (property == kABPersonPhoneProperty) {
//取出当前区域所有数据
ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);
//根据点击对应identifieer取出所在索引
int index = ABMultiValueGetIdentifierAtIndex(phoneMulti, identifier);
NSString * phone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMulti, index);
//正则过滤取出手机号
NSString * mobilePhone = [self isMobileNumber:phone];
userPhoneNumber = mobilePhone;
NSLog(@"取出手机号%@ 系统%f",mobilePhone,systemVersion);
phoneLabel.text = mobilePhone;
}
//关闭通讯录
[self dismissViewControllerAnimated:YES completion:nil];
return NO;
}
//ios 8.0之后使用
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
CGFloat systemVersion = [[UIDevice currentDevice].systemVersion floatValue];
NSString * name = (__bridge NSString *)ABRecordCopyCompositeName(person);
userName = name;
NSLog(@"取出姓名%@ 系统%f",name,systemVersion);
nameLabel.text = name;
if (property == kABPersonPhoneProperty) {
//取出对应区域msg
ABMutableMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
int index2 = ABMultiValueGetIdentifierAtIndex(phone, identifier);
NSString * phone1 = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phone,index2);
userPhoneNumber = [self isMobileNumber:phone1];
NSLog(@"取出手机号%@ 系统%f",userPhoneNumber,systemVersion);
phoneLabel.text = userPhoneNumber;
}
}
//获取调用权限(ios6之后)
- (void)accessTheAddress
{
ABAddressBookRef addRessBook = nil;
if ([[UIDevice currentDevice].systemName floatValue] >= 6.0) {
addRessBook = ABAddressBookCreateWithOptions(NULL, NULL);
//等待用户同意,向下执行
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addRessBook, ^(bool granted, CFErrorRef error) {
//
});
//dispatch_release(sema);
}else{
addRessBook = ABAddressBookCreateWithOptions(NULL, NULL);
}
}
// 正则判断从电话本调取的手机号
- (NSString )isMobileNumber:(NSString )mobileNum
{
NSString * MOBILE = @"^(?:\\+?\\d*?\\s*)?(\\d{3})-?(\\d{4})-?(\\d{4})$";
NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern:MOBILE options:NSRegularExpressionCaseInsensitive error:nil];
NSArray * matches = [regex matchesInString:mobileNum options:0 range:NSMakeRange(0, [mobileNum length])];
NSString * grop = nil;
NSString * grop1 = nil;
NSString * grop2 = nil;
NSString * grop3 = nil;
for (NSTextCheckingResult * match in matches) {
grop1 = [mobileNum substringWithRange:[match rangeAtIndex:1]];
grop2 = [mobileNum substringWithRange:[match rangeAtIndex:2]];
grop3 = [mobileNum substringWithRange:[match rangeAtIndex:3]];
}
grop = [[grop1 stringByAppendingString:grop2] stringByAppendingString:grop3];
NSLog(@"%@",grop);
if (!grop) {
return @"无";
}else{
return grop;
}
}
@end