通讯录
简介
苹果提供了读取联系人数据库的借口,通过 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];