首页 > 代码库 > iOS 访问通讯录

iOS 访问通讯录

1.构建UI

 

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

 1 - (void)viewDidLoad 2 { 3     [super viewDidLoad]; 4      5      // 请求访问通讯录的权限 6     [self requestAccessAddressBook]; 7 } 8  9 // 请求访问通讯录的权限10 - (void)requestAccessAddressBook11 {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等字眼,创建的数据类型,最终都需要release25     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)readMyAddressBookWithOC16 {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)readMyAddressBookWithC44 {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 访问通讯录