旧版的通讯录
简单介绍
ABAddressBook:通讯录
ABRecord:信息(联系人信息)
ABPerson:个人
ABGroup:组
ABMultiValue:多个数据(例如:手机号)
授权
// 授权状态
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
typedef CF_ENUM(CFIndex, ABAuthorizationStatus) {
kABAuthorizationStatusNotDetermined = 0, // 未知
kABAuthorizationStatusRestricted, // 限制
kABAuthorizationStatusDenied, // 拒绝
kABAuthorizationStatusAuthorized // 允许
};
ABAddressBookRef addressBook = NULL;
if (status == kABAuthorizationStatusNotDetermined) {
// 如果是未知状态:获取权限
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
//权限得到允许
}
});
} else {
// 其他状态,再判断提示...
}
CFRelease(addressBook);
创建通讯录
// 创建通讯录实例
ABAddressBookRef addressBook = ABAddressBookCreate();
ABAddressBookRef add = ABAddressBookCreateWithOptions(NULL, NULL);
// 创建成功 回调
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"创建通讯录成功");
}
});
// 使用通讯录实例,可以查询各种信息了
通讯录操作
- 修改信息
// 检查修改信息后,是否保存(修改 ABRecordSetValue...)
if (ABAddressBookHasUnsavedChanges(addressBook)) {
NSLog(@"存在为保存的记录");
}
// 保存
if (ABAddressBookSave(addressBook, NULL)) {
NSLog(@"保存成功");
}
- 新增 移除
ABAddressBookAddRecord(addressBook, person, NULL);
ABAddressBookRemoveRecord(addressBook, person, NULL);
通知
// 通知 通讯录变化
void (externalChangeCallback)(ABAddressBookRef addressBook, CFDictionaryRef info, void *context) {
NSLog(@"变化了?");
}
// 注册通知
ABAddressBookRegisterExternalChangeCallback(addressBook, externalChangeCallback, NULL);
// 移除通知
ABAddressBookUnregisterExternalChangeCallback(addressBook, externalChangeCallback, NULL);
其他
// 添加本地化
AB_EXTERN CFStringRef ABAddressBookCopyLocalizedLabel(CFStringRef label) AB_DEPRECATED("use [CNLabeledValue localizedStringForLabel:]");
// 回滚
AB_EXTERN void ABAddressBookRevert(ABAddressBookRef addressBook) AB_DEPRECATED("refetch CN objects");
1
网友评论