iOS开发获取手机通讯录中的电话号码和姓名

1、申请授权

#import "AppDelegate.h"
#import 

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    [self requestAuthorizationAddressBook];
    return YES;
}


- (void)requestAuthorizationAddressBook {
    // 判断是否授权
    ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus();
    if (authorizationStatus == kABAuthorizationStatusNotDetermined) {
        // 请求授权
        ABAddressBookRef addressBookRef =  ABAddressBookCreate();
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            if (granted) {  // 授权成功

            } else {        // 授权失败
                NSLog(@"授权失败!");
            }
        });
    }
}
@end


2、iOS10之后需要在Info.plist中配置NSContactsUsageDescription

NSContactsUsageDescription
请求访问通讯录 


3、请求访问并获取通讯录

3.1、导入头文件

#import

3.2、访问并获取

- (void)getAddressBookClick{
    
    ABAddressBookRef addressBook = ABAddressBookCreate();
    //用户授权
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {//首次访问通讯录
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            if (!error) {
                if (granted) {//允许
                    NSArray *contacts = [self fetchContactWithAddressBook:addressBook];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        NSLog(@"\n contacts:%@", contacts);
                    });
                }else{//拒绝
                    NSLog(@"拒绝");
                }
            }else{
                NSLog(@"错误!");
            }
        });
    }else{//非首次访问通讯录
       
        NSArray *contacts = [self fetchContactWithAddressBook:addressBook];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"\n contacts:%@", contacts);
        });
    }
}

- (NSMutableArray *)fetchContactWithAddressBook:(ABAddressBookRef)addressBook{
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {////有权限访问
        //获取联系人数组
        NSArray *array = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
        NSMutableArray *contacts = [NSMutableArray array];
        for (int i = 0; i < array.count; i++) {
            //获取联系人
            ABRecordRef people = CFArrayGetValueAtIndex((__bridge ABRecordRef)array, i);
            //获取联系人详细信息,如:姓名,电话,住址等信息
            NSString *firstName = (__bridge NSString *)ABRecordCopyValue(people, kABPersonFirstNameProperty);
            NSString *lastName = (__bridge NSString *)ABRecordCopyValue(people, kABPersonLastNameProperty);
        
            //判断姓名null
            NSString *allName;
            if ([CommandHelp checkStringIsNull:lastName] && [CommandHelp checkStringIsNull:firstName]) {
                allName = [NSString stringWithFormat:@"%@%@",lastName,firstName];
            }else if([CommandHelp checkStringIsNull:firstName]){
                allName = firstName;
            }else if ([CommandHelp checkStringIsNull:lastName]){
                allName = lastName;
            }else{
                allName = @"";
            }
            
            ABMutableMultiValueRef phoneNumRef = ABRecordCopyValue(people, kABPersonPhoneProperty);
            NSString *phoneNumber =  ((__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phoneNumRef)).lastObject;
            //判断手机号null
            NSString *phone;
            if ([CommandHelp checkStringIsNull:phoneNumber]) {
                phone = phoneNumber;
            }else{
                phone = @"";
            }
            
            //如果不加上面的判断,这里加入数组的时候会出错,不会判断(null)这个东西,所以要先排除
            [contacts addObject:@{@"name": allName, @"phoneNumber": phone}];
         
        }
        return contacts;
    }else{//无权限访问
        //提示授权
        UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您设置允许APP访问您的通讯录\n设置-隐私-通讯录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alart show];
        return nil;
    }
}



4、获取数据示例

iOS开发获取手机通讯录中的电话号码和姓名_第1张图片

你可能感兴趣的:(iOS)