@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 判断是否授权成功
if(ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
{
// 授权成功直接返回
return;
}
// 0.创建通讯录
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 1.请求用户授权
// 第一个参数接收通讯录
// 第二个参数是一个block, 无论授权成功还是失败都会调用
ABAddressBookRequestAccessWithCompletion(book, ^(bool granted, CFErrorRef error) {
// granted YES 代表用户授权成功 NO 代表用户授权失败
if (granted) {
NSLog(@"授权成功");
}else
{
NSLog(@"授权失败");
}
});
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 判断是否授权成功
if(ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized)
{
// 授权失败直接返回
return;
}
// 1.创建通讯录对象
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 2.获取通讯录中得所有联系人
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(book);
CFIndex count = CFArrayGetCount(allPeople);
// 3.打印每一个联系人额信息
for (int i = 0; i < count; i++) {
// 联系人列表中的每一个人都是一个ABrecordRef
ABRecordRef prople = CFArrayGetValueAtIndex(allPeople, i);
//取出当前联系人的的电话信息
// 获取练习人得姓名
CFStringRef lastName = ABRecordCopyValue(prople, kABPersonLastNameProperty);
CFStringRef firstName = ABRecordCopyValue(prople, kABPersonFirstNameProperty);
NSLog(@"%@ %@", firstName, lastName);
// 获取联系人的电话
// 从联系人中获取到得电话是所有的电话
ABMultiValueRef phones = ABRecordCopyValue(prople, kABPersonPhoneProperty);
// 获取当前联系人总共有多少种电话
CFIndex phoneCount = ABMultiValueGetCount(phones);
for (int i = 0; i < phoneCount; i++) {
CFStringRef name = ABMultiValueCopyLabelAtIndex(phones, i);
// 从所有的电话中取出指定的电话
CFStringRef value = ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"name = %@ value = %@", name, value);
}
}
}
@end
//通讯录的其他操作
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 判断是否授权成功
if(ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
{
// 授权成功直接返回
return;
}
// 0.创建通讯录
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 1.请求用户授权
// 第一个参数接收通讯录
// 第二个参数是一个block, 无论授权成功还是失败都会调用
ABAddressBookRequestAccessWithCompletion(book, ^(bool granted, CFErrorRef error) {
// granted YES 代表用户授权成功 NO 代表用户授权失败
if (granted) {
NSLog(@"授权成功");
}else
{
NSLog(@"授权失败");
}
});
}
// 读取练习人信息
- (IBAction)readRecord:(id)sender
{
// 判断是否授权成功
if(ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized)
{
// 授权失败直接返回
return;
}
// 1.创建通讯录对象
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 2.获取通讯录中得所有联系人
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(book);
CFIndex count = CFArrayGetCount(allPeople);
// 3.打印每一个联系人额信息
for (int i = 0; i < count; i++) {
// 联系人列表中的每一个人都是一个ABrecordRef
ABRecordRef prople = CFArrayGetValueAtIndex(allPeople, i);
//取出当前联系人的的电话信息
// 获取练习人得姓名
CFStringRef lastName = ABRecordCopyValue(prople, kABPersonLastNameProperty);
CFStringRef firstName = ABRecordCopyValue(prople, kABPersonFirstNameProperty);
NSLog(@"%@ %@", firstName, lastName);
// 获取联系人的电话
// 从联系人中获取到得电话是所有的电话
ABMultiValueRef phones = ABRecordCopyValue(prople, kABPersonPhoneProperty);
// 获取当前联系人总共有多少种电话
CFIndex phoneCount = ABMultiValueGetCount(phones);
for (int i = 0; i < phoneCount; i++) {
CFStringRef name = ABMultiValueCopyLabelAtIndex(phones, i);
// 从所有的电话中取出指定的电话
CFStringRef value = ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"name = %@ value = %@", name, value);
}
}
}
// 更新联系人
- (IBAction)updateRecord:(id)sender
{
// 判断是否授权成功
if(ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized)
{
// 授权失败直接返回
return;
}
// 1.创建通讯录对象
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 2.获取通讯录中得所有联系人
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(book);
// 3.取出其中的一个联系人
ABRecordRef people = CFArrayGetValueAtIndex(allPeople, 0);
// 4.修改联系人信息
ABRecordSetValue(people, kABPersonLastNameProperty, @"牛", NULL);
// 5.保存修改之后的信息
ABAddressBookSave(book, NULL);
}
- (IBAction)addRecord:(id)sender
{
// 1.创建联系人
ABRecordRef people = ABPersonCreate();
// 2.设置联系人信息
ABRecordSetValue(people, kABPersonLastNameProperty, @"牛", NULL);
ABRecordSetValue(people, kABPersonFirstNameProperty, @"查", NULL);
// 创建电话号码
ABMultiValueRef phones = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(phones, @"123456789", kABPersonPhoneMainLabel, NULL);
ABMultiValueAddValueAndLabel(phones, @"8888888", kABPersonPhoneHomeFAXLabel, NULL);
ABRecordSetValue(people, kABPersonPhoneProperty, phones, NULL);
// 3.拿到通讯录
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 4.将联系人添加到通讯录中
ABAddressBookAddRecord(book, people, NULL);
// 5.保存通讯录
ABAddressBookSave(book, NULL);
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
// 判断是否授权成功
if(ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
{
// 授权成功直接返回
return;
}
// 0.创建通讯录
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 1.请求用户授权
// 第一个参数接收通讯录
// 第二个参数是一个block, 无论授权成功还是失败都会调用
ABAddressBookRequestAccessWithCompletion(book, ^(bool granted, CFErrorRef error) {
// granted YES 代表用户授权成功 NO 代表用户授权失败
if (granted) {
NSLog(@"授权成功");
}else
{
NSLog(@"授权失败");
}
});
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 判断是否授权成功
if(ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized)
{
// 授权失败直接返回
return;
}
// 1.创建通讯录对象
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 2.获取通讯录中得所有联系人
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(book);
CFIndex count = CFArrayGetCount(allPeople);
// 3.打印每一个联系人额信息
for (int i = 0; i < count; i++) {
// 联系人列表中的每一个人都是一个ABrecordRef
ABRecordRef prople = CFArrayGetValueAtIndex(allPeople, i);
//取出当前联系人的的电话信息
// 获取练习人得姓名
CFStringRef lastName = ABRecordCopyValue(prople, kABPersonLastNameProperty);
CFStringRef firstName = ABRecordCopyValue(prople, kABPersonFirstNameProperty);
NSLog(@"%@ %@", firstName, lastName);
// 获取联系人的电话
// 从联系人中获取到得电话是所有的电话
ABMultiValueRef phones = ABRecordCopyValue(prople, kABPersonPhoneProperty);
// 获取当前联系人总共有多少种电话
CFIndex phoneCount = ABMultiValueGetCount(phones);
for (int i = 0; i < phoneCount; i++) {
CFStringRef name = ABMultiValueCopyLabelAtIndex(phones, i);
// 从所有的电话中取出指定的电话
CFStringRef value = ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"name = %@ value = %@", name, value);
}
}
}
@end
//通讯录的其他操作
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 判断是否授权成功
if(ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
{
// 授权成功直接返回
return;
}
// 0.创建通讯录
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 1.请求用户授权
// 第一个参数接收通讯录
// 第二个参数是一个block, 无论授权成功还是失败都会调用
ABAddressBookRequestAccessWithCompletion(book, ^(bool granted, CFErrorRef error) {
// granted YES 代表用户授权成功 NO 代表用户授权失败
if (granted) {
NSLog(@"授权成功");
}else
{
NSLog(@"授权失败");
}
});
}
// 读取练习人信息
- (IBAction)readRecord:(id)sender
{
// 判断是否授权成功
if(ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized)
{
// 授权失败直接返回
return;
}
// 1.创建通讯录对象
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 2.获取通讯录中得所有联系人
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(book);
CFIndex count = CFArrayGetCount(allPeople);
// 3.打印每一个联系人额信息
for (int i = 0; i < count; i++) {
// 联系人列表中的每一个人都是一个ABrecordRef
ABRecordRef prople = CFArrayGetValueAtIndex(allPeople, i);
//取出当前联系人的的电话信息
// 获取练习人得姓名
CFStringRef lastName = ABRecordCopyValue(prople, kABPersonLastNameProperty);
CFStringRef firstName = ABRecordCopyValue(prople, kABPersonFirstNameProperty);
NSLog(@"%@ %@", firstName, lastName);
// 获取联系人的电话
// 从联系人中获取到得电话是所有的电话
ABMultiValueRef phones = ABRecordCopyValue(prople, kABPersonPhoneProperty);
// 获取当前联系人总共有多少种电话
CFIndex phoneCount = ABMultiValueGetCount(phones);
for (int i = 0; i < phoneCount; i++) {
CFStringRef name = ABMultiValueCopyLabelAtIndex(phones, i);
// 从所有的电话中取出指定的电话
CFStringRef value = ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"name = %@ value = %@", name, value);
}
}
}
// 更新联系人
- (IBAction)updateRecord:(id)sender
{
// 判断是否授权成功
if(ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized)
{
// 授权失败直接返回
return;
}
// 1.创建通讯录对象
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 2.获取通讯录中得所有联系人
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(book);
// 3.取出其中的一个联系人
ABRecordRef people = CFArrayGetValueAtIndex(allPeople, 0);
// 4.修改联系人信息
ABRecordSetValue(people, kABPersonLastNameProperty, @"牛", NULL);
// 5.保存修改之后的信息
ABAddressBookSave(book, NULL);
}
- (IBAction)addRecord:(id)sender
{
// 1.创建联系人
ABRecordRef people = ABPersonCreate();
// 2.设置联系人信息
ABRecordSetValue(people, kABPersonLastNameProperty, @"牛", NULL);
ABRecordSetValue(people, kABPersonFirstNameProperty, @"查", NULL);
// 创建电话号码
ABMultiValueRef phones = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(phones, @"123456789", kABPersonPhoneMainLabel, NULL);
ABMultiValueAddValueAndLabel(phones, @"8888888", kABPersonPhoneHomeFAXLabel, NULL);
ABRecordSetValue(people, kABPersonPhoneProperty, phones, NULL);
// 3.拿到通讯录
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
// 4.将联系人添加到通讯录中
ABAddressBookAddRecord(book, people, NULL);
// 5.保存通讯录
ABAddressBookSave(book, NULL);
}
@end