码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 访问通讯录

时间:2014-12-16 06:27:40      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   io   color   os   sp   for   on   

1.构建UI

bubuko.com,布布扣

 

2.向用户申请通讯录的授权 

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     
 5      // 请求访问通讯录的权限
 6     [self requestAccessAddressBook];
 7 }
 8 
 9 // 请求访问通讯录的权限
10 - (void)requestAccessAddressBook
11 {
12     // 创建通讯录实例对象
13     ABAddressBookRef addressbook = ABAddressBookCreateWithOptions(NULL, NULL);
14     
15     // 向用户请求访问通讯录
16     ABAddressBookRequestAccessWithCompletion(addressbook, ^(bool granted, CFErrorRef error) {
17         if (granted) {
18             NSLog(@"允许访问通讯录");
19         }else{
20             NSLog(@"不允许访问通讯录");
21         };
22     });
23     
24     // 释放资源, 凡是函数名中包含了create\retain\copy\new等字眼,创建的数据类型,最终都需要release
25     CFRelease(addressbook);
26 }

 

3.访问通讯录

 1 - (IBAction)readAddressBook {
 2     [self readMyAddressBookWithC];
 3 //    [self readMyAddressBookWithOC];
 4 }
 5 
 6 /*
 7   typedef CF_ENUM(CFIndex, ABAuthorizationStatus) {
 8       kABAuthorizationStatusNotDetermined = 0, // 用户还没做决定
 9       kABAuthorizationStatusRestricted, // iOS设备上的家长控制或其它一些许可配置阻止了你的程序与通讯录数据库进行交互
10       kABAuthorizationStatusDenied, // 用户明确拒绝
11       kABAuthorizationStatusAuthorized // 经授权
12   };
13 */
14 
15 - (void)readMyAddressBookWithOC
16 {
17     // 判断授权
18     if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) return;
19     
20     // 创建通讯录实例对象
21     ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
22     
23     // 获得通讯录的所有联系人,并利用桥接把Core Foundation的数据类型 --> Foundation数据类型
24     NSArray *allContacts = (__bridge NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);
25     
26     // 遍历数组中的联系人
27     for (int i = 0; i < allContacts.count; i++) {
28         
29         // 从数组中取出i位置的联系人
30         ABRecordRef contact = (__bridge ABRecordRef)allContacts[i];
31         
32         // 获取该联系人的详细信息
33         NSString *lastName = (__bridge NSString *)ABRecordCopyValue(contact, kABPersonLastNameProperty);
34         NSString *firstName = (__bridge NSString *)ABRecordCopyValue(contact, kABPersonFirstNameProperty);
35         
36         // 输出
37         NSLog(@"姓:%@ , 名:%@ ",lastName,firstName);
38     }
39     // 释放
40     CFRelease(addressBook);
41 }
42 
43 - (void)readMyAddressBookWithC
44 {
45     // 1.判断授权
46     if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) return;
47     
48     // 2.创建通讯录实例对象
49     ABAddressBookRef addressbook = ABAddressBookCreateWithOptions(NULL, NULL);
50     
51     // 3.获得通讯录中的所有联系人
52     CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(addressbook);
53     
54     // 4.遍历数组中的所有联系人
55     CFIndex count = CFArrayGetCount(allContacts);
56     for (CFIndex i = 0; i < count; i++) {
57         
58         // 5.获得i这个位置对应的联系人(1个联系人 对应 1个 ABRecordRef)
59         ABRecordRef contact = CFArrayGetValueAtIndex(allContacts, i);
60         
61         // 6.获取更详细的数据类型
62         CFStringRef firstName = ABRecordCopyValue(contact, kABPersonFirstNameProperty);
63         CFStringRef lastName = ABRecordCopyValue(contact, kABPersonLastNameProperty);
64         ABMultiValueRef phone = ABRecordCopyValue(contact, kABPersonPhoneProperty);
65         
66         CFArrayRef phoneArray = ABMultiValueCopyArrayOfAllValues(phone);
67         CFIndex phoneCount = CFArrayGetCount(phoneArray);
68         for (CFIndex j= 0;j < phoneCount ; j++) {
69             CFStringRef phoneName =  ABMultiValueCopyLabelAtIndex(phone, j);
70             CFStringRef phoneNum = ABMultiValueCopyValueAtIndex(phone, j);
71             NSLog(@"号码类型:%@ ,号码:%@",phoneName,phoneNum);
72             CFRelease(phoneName);
73             CFRelease(phoneNum);
74         }
75         
76         // 输出
77         NSLog(@"姓:%@ ,名:%@",lastName,firstName);
78         
79         // 释放
80         CFRelease(firstName);
81         CFRelease(lastName);
82         CFRelease(phone);
83         CFRelease(phoneArray);
84     }
85     // 释放
86     CFRelease(addressbook);
87     CFRelease(allContacts);
88 }

 

4.更新联系人

 1 - (IBAction)updateAddressBook {
 2     // 0.判断授权
 3     if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) return;
 4     
 5     // 1.创建通讯录实例对象
 6     ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
 7     
 8     // 2.获得通讯录的所有联系人,并利用桥接把Core Foundation的数据类型 --> Foundation数据类型
 9     NSArray *allContacts = (__bridge NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);
10     
11     // 3.获得数组中的第0个人
12     ABRecordRef contact = (__bridge ABRecordRef)(allContacts[0]);
13     CFStringRef lastName = (__bridge CFStringRef)@"";
14     // 4.修改姓
15     ABRecordSetValue(contact, kABPersonLastNameProperty, lastName, NULL);
16     
17     // 5.同步
18     ABAddressBookSave(addressBook, NULL);
19     
20 }

 

5.新增联系人

 1 - (IBAction)addAddressBook {
 2     // 0.判断授权
 3     if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) return;
 4     
 5     // 1.创建通讯录实例对象
 6     ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
 7     
 8     // 2.创建联系人(内部数据空)
 9     ABRecordRef newContact = ABPersonCreate();
10     
11     // 3. 设置姓名
12     ABRecordSetValue(newContact, kABPersonFirstNameProperty, (__bridge CFStringRef)@"", NULL);
13     ABRecordSetValue(newContact, kABPersonLastNameProperty, (__bridge CFStringRef)@"大锤", NULL);
14     
15     // 4.设置电话号码
16     ABMultiValueRef phone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
17     ABMultiValueAddValueAndLabel(phone, (__bridge CFStringRef)@"10010", kABPersonPhoneMainLabel, NULL);
18     ABMultiValueAddValueAndLabel(phone, (__bridge CFStringRef)@"10011", kABPersonPhoneMobileLabel, NULL);
19     ABMultiValueAddValueAndLabel(phone, (__bridge CFStringRef)@"10012", kABPersonPhoneIPhoneLabel, NULL);
20     ABRecordSetValue(newContact, kABPersonPhoneProperty, phone, NULL);
21     
22     // 5.添加新联系人到通讯录
23     ABAddressBookAddRecord(addressBook, newContact, NULL);
24     
25     // 6.同步
26     ABAddressBookSave(addressBook, NULL);
27     
28     // 4.释放
29     CFRelease(newContact);
30     CFRelease(addressBook);
31     CFRelease(phone);
32 }

 

iOS 访问通讯录

标签:style   blog   ar   io   color   os   sp   for   on   

原文地址:http://www.cnblogs.com/oumygade/p/4166242.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!