iOS开发学习之路【高级主题】——通讯录

通讯录

简介

苹果提供了读取联系人数据库的借口,通过 AddressBook.framework 框架中的API来实现。

检测是否允许访问

因为涉及到私密信息,需要先取得权限。参考我之前的博客。https://blog.csdn.net/guyindong/article/details/89400282

		/*typedef CF_ENUM(CFIndex, ABAuthorizationStatus) {
        kABAuthorizationStatusNotDetermined = 0,    // deprecated, use CNAuthorizationStatusNotDetermined
        kABAuthorizationStatusRestricted,           // deprecated, use CNAuthorizationStatusRestricted
        kABAuthorizationStatusDenied,               // deprecated, use CNAuthorizationStatusDenied
        kABAuthorizationStatusAuthorized            // deprecated, use CNAuthorizationStatusAuthorized
    } AB_DEPRECATED("use CNAuthorizationStatus");
     */
    
    ABAddressBookRef ref = ABAddressBookCreateWithOptions(nil, nil);
    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
    
    
    switch (status) {
        case kABAuthorizationStatusNotDetermined:
            ABAddressBookRequestAccessWithCompletion(ref, ^(bool granted, CFErrorRef error) {
                if (granted) {
                    NSLog(@"允许访问...");
                }
            });
            break;
            
        case kABAuthorizationStatusDenied:
            NSLog(@"拒绝访问...");
            break;
            
        case kABAuthorizationStatusAuthorized:
            NSLog(@"允许访问...");
            break;
            
        default:
            break;
    }

读取所有联系人

-(void)query:(ABAddressBookRef)ref{
    
    NSArray *array = (__bridge NSArray *)(ABAddressBookCopyArrayOfAllPeople(ref));
    for (int i = 0; i < array.count; i++) {
        // 获取联系人信息
        ABRecordRef per = (__bridge ABRecordRef)([array objectAtIndex:i]);
        // 获取lastName
        NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(per, kABPersonLastNameProperty));
        NSLog(@"lastName = %@", lastName);
        // 获取电话号码,因为电话号码有多个,所以返回
        ABMultiValueRef phone = ABRecordCopyValue(per, kABPersonPhoneProperty);
        NSUInteger j = 0;
        for (j = 0; j < ABMultiValueGetCount(phone); j++) {
            //  获得电话号码标签,例如:手机、办公电话等
            NSString *phoneLabel = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(phone, j));
            //  获得电话号码
            NSString *phoneNumber = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phone, j));
            NSLog(@"Label = %@, phoneNumber = %@",phoneLabel,phoneNumber);
            
        }
    }
    
}

在这里插入图片描述

添加联系人

-(void)add:(ABAddressBookRef)ref{
    // 创建一个Person记录
    ABRecordRef result = ABPersonCreate();
    
    BOOL couldSetName = NO;
    BOOL couldSetPhone = NO;
    NSString *name = @"cobe";
    // 设置lastname
    couldSetName = ABRecordSetValue(result, kABPersonLastNameProperty, (__bridge CFTypeRef)name, NULL);
    // 设置电话号码
    
    // 添加新纪录
    BOOL couldAddPerson = ABAddressBookAddRecord(ref, result, nil);
    if(couldAddPerson){
        NSLog(@"添加成功!");
    } else {
        NSLog(@"添加失败!");
        CFRelease(result);
        result = NULL;
    }
    // 判断是否有未保存的内容
    if (ABAddressBookHasUnsavedChanges(ref)) {
        BOOL couldSaveAddressBook = ABAddressBookSave(ref, nil);
        if (couldSaveAddressBook) {
            NSLog(@"success...");
        }
    }
}

在这里插入图片描述
在这里插入图片描述

使用视图控制器访问通讯录

	  // 显示通讯录导航,页面跳转
		ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc]init];
    picker.peoplePickerDelegate = self;
    [self presentModalViewController:picker animated:YES];

		// 显示个人信息
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
    ABPersonViewController *view = [[ABPersonViewController alloc]init];
    view.personViewDelegate = self;
    view.allowsEditing = YES;
    view.displayedPerson = person;
    [peoplePicker pushViewController:view animated:YES];
}

    // 创建新的联系人
    ABNewPersonViewController *view = [[ABNewPersonViewController alloc]init];
    view.newPersonViewDelegate = self;
    UINavigationController *newNavigationController = [[UINavigationController alloc]initWithRootViewController:view];
    [self presentModalViewController:newNavigationController animated:YES];
		
		// 修改已有联系人
    ABUnknownPersonViewController *view = [[ABUnknownPersonViewController alloc]init];
    view.unknownPersonViewDelegate = self;
    view.displayedPerson = person; // Assume person is already defined.
    view.allowsAddingToAddressBook = YES;
    [self.navigationController pushViewController:view animated:YES];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值