获取通讯录-第三方框架RHAddressBook(OC)

  1. 集成框架
    1. 将整个工程拖入项目
    2. 添加工程依赖
    Build Phases -> Target Dependencies -> +
    3. 添加链接项
    Build Settings -> Other Linker Flags -> -ObjC -all_load
    4. 导入框架头文件

    #import 
    
  2. 代码实现

    // 2. 使用框架获取所有联系人信息
    
    // 2.1 请求授权
        // 1. 获取授权状态
        RHAuthorizationStatus status = [RHAddressBook authorizationStatus];
    
        if (status == RHAuthorizationStatusNotDetermined)
        {
            // 2. 创建通讯录对象
            RHAddressBook *addressBook = [[RHAddressBook alloc] init];
    
            // 3. 请求授权
            [addressBook requestAuthorizationWithCompletion:^(bool granted, NSError *error) {
            if (granted)
            {
                NSLog(@"授权成功!");
            }
            else
            {
                NSLog(@"授权失败");
            }
            }];
        }
    
    // 2.2 获取联系人信息
    
        // 1. 判断当前授权状态
        RHAuthorizationStatus status = [RHAddressBook authorizationStatus];
        if (status != RHAuthorizationStatusAuthorized) {
            return;
        }
    
        // 2. 创建通讯录对象
        RHAddressBook *addressBook = [[RHAddressBook alloc] init];
    
        // 3. 获取所有联系人
        NSArray *peoples = addressBook.people;
    
        // 4. 遍历所有联系人
        for (RHPerson *person in peoples)
        {
            // 4.1 获取联系人姓名
            NSString *firstName = person.firstName;
            NSString *lastName = person.lastName;
            NSLog(@"%@---%@", firstName, lastName);
    
            // 4.2 获取联系人电话
            RHMultiStringValue *mv = person.phoneNumbers;
            for (int i = 0; i < mv.count; i ++)
            {
                // 4.2.1 获取电话标签
                NSString *label = [mv labelAtIndex:i];
                // NSString *label = [mv localizedLabelAtIndex:i];
    
                // 4.2.2 获取电话号码
                NSString *phone = [mv valueAtIndex:i];
                NSLog(@"%@--%@", label, phone);
    
            }
        }
    

你可能感兴趣的:(获取通讯录-第三方框架RHAddressBook(OC))