ios开发 之 通讯录操作

原文地址:http://blog.csdn.net/zeng_zhiming/article/details/70141316

 

在ios开发中,  通讯录操作还是比较常见的, 尤其是在社交项目中更为突出.  目前系统提供了两个框架对通讯录进行操作:

 

1. 兼容iOS9以前的框架

#import 

2.iOS9以后的新框架

#import 

 

为了项目能兼容老版本, 本文主要整理的使用:

 

一.  判断通讯录权限

 

 
  1. // 判断权限

  2. switch (ABAddressBookGetAuthorizationStatus()) {

  3. case kABAuthorizationStatusNotDetermined: //!< 未选择权限.

  4. {

  5.  
  6. }

  7. break;

  8. case kABAuthorizationStatusRestricted: //!< 权限被限制.

  9. {

  10.  
  11. }

  12. break;

  13. case kABAuthorizationStatusDenied: //!< 已拒绝权限.

  14. {

  15.  
  16. }

  17. break;

  18. case kABAuthorizationStatusAuthorized: //!< 已授权.

  19. {

  20.  
  21. }

  22. break;

  23.  
  24. default:

  25. break;

  26. }

 

 

二. 请求通讯录访问权限

 

 

 
  1. /**

  2. 请求通讯录访问权限

  3.  
  4. @param completion 权限回调

  5. */

  6. - (void)requestAddressBookAccessWithCompletion:(void (^)(int code, NSString *msg))completion

  7. {

  8. // 请求访问用户通讯录,注意无论成功与否block都会调用

  9. ABAddressBookRequestAccessWithCompletion(_addressBook, ^(bool granted, CFErrorRef error) {

  10. // 回调到主线程返回结果

  11. dispatch_async(dispatch_get_main_queue(), ^{

  12. if (granted) {

  13. if (completion) completion(1, @"请求通讯录访问权限成功");

  14. } else {

  15. if (completion) completion(-1, @"请求通讯录访问权限失败");

  16. }

  17. });

  18. });

  19. }

 

 

三.  通讯录操作

1.  获取通讯录联系人列表

 

 
  1. /**

  2. * 同步获取通讯录联系人列表

  3. *

  4. * @return 通讯录ZMPersonModel数组

  5. */

  6. - (NSArray *)getAddressBookPersonListWithSort:(PersonSort)personSort

  7. {

  8. // 检测权限

  9. if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) {

  10. return nil;

  11. }

  12.  
  13. // 按照排序读取所有联系人

  14. CFArrayRef allPerson = NULL;

  15. switch (personSort) {

  16. case SortByFirstName: //!< 名字排序.

  17. allPerson = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(_addressBook, kABSourceTypeLocal, kABPersonSortByFirstName);

  18. break;

  19. case SortByLastName: //!< 姓氏排序.

  20. allPerson = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(_addressBook, kABSourceTypeLocal, kABPersonSortByLastName);

  21. break;

  22. case SortByCreationTime://!< 创建时间排序.

  23. default:

  24. allPerson = ABAddressBookCopyArrayOfAllPeople(_addressBook);

  25. break;

  26. }

  27.  
  28. // 存储通讯录的数组

  29. NSMutableArray *addressBookArray = [NSMutableArray array];

  30.  
  31. // 循环遍历,获取每个联系人信息

  32. for (NSInteger i = 0; i < CFArrayGetCount(allPerson); i++) {

  33. ABRecordRef person = CFArrayGetValueAtIndex(allPerson, i);

  34. ZMPersonModel *personModel = [[ZMPersonModel alloc] initWithPerson:person];

  35. [addressBookArray addObject:personModel];

  36. }

  37.  
  38. // 释放资源

  39. if (allPerson) CFRelease(allPerson);

  40.  
  41. return addressBookArray;

  42. }

 

 

2. 根据RecordID查找联系人

 

 

 
  1. /**

  2. 根据RecordID查找联系人

  3.  
  4. @param recordID 联系人ID

  5. @return ZMPersonModel对象

  6. */

  7. - (ZMPersonModel *)getPersonWithRecordID:(ABRecordID)recordID

  8. {

  9. // 获取Person

  10. ABRecordRef person = ABAddressBookGetPersonWithRecordID(_addressBook, recordID);

  11.  
  12. // 初始化为ZMPersonModel

  13. ZMPersonModel *personModel = [[ZMPersonModel alloc] initWithPerson:person];

  14.  
  15. return personModel;

  16. }

 

 

3. 根据Name查找联系人

 

 

 
  1. /**

  2. 根据Name查找联系人

  3.  
  4. @param name 查找关键字

  5. @return ZMPersonModel数组

  6. */

  7. - (NSArray *)getPersonsWithName:(NSString *)name

  8. {

  9. // 空值过滤

  10. if (name == nil || name.length == 0) {

  11. return nil;

  12. }

  13.  
  14. // 根据Name获取联系人

  15. CFArrayRef persons = ABAddressBookCopyPeopleWithName(_addressBook, (__bridge CFStringRef)(name));

  16.  
  17. // 存储通讯录的数组

  18. NSMutableArray *personArray = [NSMutableArray array];

  19.  
  20. // 循环遍历,获取每个联系人信息

  21. for (NSInteger i = 0; i < CFArrayGetCount(persons); i++) {

  22. ABRecordRef person = CFArrayGetValueAtIndex(persons, i);

  23. ZMPersonModel *personModel = [[ZMPersonModel alloc] initWithPerson:person];

  24. [personArray addObject:personModel];

  25. }

  26.  
  27. // 释放资源

  28. if (persons) CFRelease(persons);

  29.  
  30. return personArray;

  31. }

 

 

4. 添加联系到通讯录

 

 

 
  1. /**

  2. 批量添加联系到通讯录

  3.  
  4. @param personArray ZMPersonModel数组

  5. @return 添加结果

  6. */

  7. - (BOOL)addPersons:(NSArray *)personArray

  8. {

  9. // 空值过滤

  10. if (personArray == nil || personArray.count == 0) {

  11. return NO;

  12. }

  13.  
  14. // 添加结果

  15. BOOL result = YES;

  16.  
  17. // 遍历数组将联系人添加到通讯录中

  18. for (ZMPersonModel *personModel in personArray)

  19. {

  20. // 实例化CFErrorRef

  21. CFErrorRef errorRef = NULL;

  22.  
  23. // 添加联系人

  24. if (ABAddressBookAddRecord(_addressBook, [personModel getRecordRef], &errorRef) == true) {

  25. // 保存通讯录

  26. if (!ABAddressBookSave(_addressBook, &errorRef)) {

  27. result = NO;

  28. }

  29. }

  30.  
  31. // 错误消息处理

  32. if (errorRef) {

  33. NSLog(@"Add Person Error:%@", errorRef);

  34. CFRelease(errorRef);

  35. }

  36. }

  37.  
  38. return result;

  39. }

 

 

5. 修改联系人

 

 

 
  1. /**

  2. 更新联系人

  3.  
  4. @param personModel ZMPersonModel对象

  5. @return 更新结果

  6. */

  7. - (BOOL)updatePerson:(ZMPersonModel *)personModel

  8. {

  9. BOOL result = NO;

  10.  
  11. // 空值过滤

  12. if (personModel == nil) {

  13. return result;

  14. }

  15.  
  16. // 实例化CFErrorRef

  17. CFErrorRef errorRef = NULL;

  18.  
  19. // 获取Person

  20. ABRecordRef person = ABAddressBookGetPersonWithRecordID(_addressBook, personModel.recordID);

  21.  
  22. // 将Model中数据更新到person中

  23. if ([personModel updateToRecordRef:person]) {

  24. // 保存通讯录

  25. result = ABAddressBookSave(_addressBook, &errorRef);

  26. }

  27.  
  28. // 错误消息处理

  29. if (errorRef) {

  30. NSLog(@"Add Person Error:%@", errorRef);

  31. CFRelease(errorRef);

  32. }

  33.  
  34. return result;

  35. }

 

 

6. 删除联系人

 

 

 
  1. /**

  2. 根据RecordID删除联系人

  3.  
  4. @param recordID 联系人ID

  5. @return 删除结果

  6. */

  7. - (BOOL)removePersonWithRecordID:(ABRecordID)recordID

  8. {

  9. BOOL result = NO;

  10.  
  11. // 实例化CFErrorRef

  12. CFErrorRef errorRef = NULL;

  13.  
  14. // 获取Person

  15. ABRecordRef person = ABAddressBookGetPersonWithRecordID(_addressBook, recordID);

  16. if ((person != NULL) && ABAddressBookRemoveRecord(_addressBook, person, &errorRef)) {

  17. // 保存通讯录

  18. if (ABAddressBookSave(_addressBook, &errorRef)) {

  19. result = YES;

  20. }

  21. }

  22.  
  23. // 错误消息处理

  24. if (errorRef) {

  25. NSLog(@"Remove Person Error:%@", errorRef);

  26. CFRelease(errorRef);

  27. }

  28.  
  29. return result;

  30. }

 

 

四. 联系人数据解析

 

1. 解析联系人所以数据

 

 
  1. /**

  2. 设置属性值

  3. */

  4. - (void)setValueWithPerson:(ABRecordRef)person

  5. {

  6. // 空值过滤

  7. if (person == NULL) {

  8. return;

  9. }

  10.  
  11. /** 获取RecordID */

  12. _recordID = ABRecordGetRecordID(person);

  13.  
  14. /** 姓名相关 */

  15. _firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty)); //!< 名字.

  16. _lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty)); //!< 姓氏.

  17. _middleName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonMiddleNameProperty));//!< 中间名.

  18. _prefix = CFBridgingRelease(ABRecordCopyValue(person, kABPersonPrefixProperty)); //!< 前缀.

  19. _suffix = CFBridgingRelease(ABRecordCopyValue(person, kABPersonSuffixProperty)); //!< 后缀.

  20. _nickname = CFBridgingRelease(ABRecordCopyValue(person, kABPersonNicknameProperty)); //!< 昵称.

  21. _firstNamePhonetic = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty)); //!< 名字拼音或音标.

  22. _lastNamePhonetic = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty)); //!< 姓氏拼音或音标.

  23. _middleNamePhonetic = CFBridgingRelease(ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty));//!< 中间名拼音或音标.

  24.  
  25. /** 工作相关 */

  26. _organization = CFBridgingRelease(ABRecordCopyValue(person, kABPersonOrganizationProperty)); //!< 公司名称.

  27. _department = CFBridgingRelease(ABRecordCopyValue(person, kABPersonDepartmentProperty)); //!< 部门.

  28. _jobTitle = CFBridgingRelease(ABRecordCopyValue(person, kABPersonJobTitleProperty)); //!< 职位.

  29.  
  30. /** 备注 */

  31. _note = CFBridgingRelease(ABRecordCopyValue(person, kABPersonNoteProperty)); //!< 备注.

  32.  
  33. /** 日期相关 */

  34. _birthday = CFBridgingRelease(ABRecordCopyValue(person, kABPersonBirthdayProperty)); //!< 生日.

  35. _creationDate = CFBridgingRelease(ABRecordCopyValue(person, kABPersonCreationDateProperty)); //!< 创建时间.

  36. _modificationDate = CFBridgingRelease(ABRecordCopyValue(person, kABPersonModificationDateProperty)); //!< 最近修改时间.

  37.  
  38. /** 农历生日 */

  39.  
  40. NSDictionary *brithdayDict = CFBridgingRelease(ABRecordCopyValue(person, kABPersonAlternateBirthdayProperty));

  41. if (!IsNullDictionary(brithdayDict))

  42. {

  43. ZMAlternateBirthdayModel *brithdayModel = [[ZMAlternateBirthdayModel alloc] init];

  44. // 标志

  45. brithdayModel.calendar = [brithdayDict valueForKey:(NSString *)kABPersonAlternateBirthdayCalendarIdentifierKey];

  46. // 纪元

  47. brithdayModel.era = [[brithdayDict valueForKey:(NSString *)kABPersonAlternateBirthdayEraKey] integerValue];

  48. // 年份

  49. brithdayModel.year = [[brithdayDict valueForKey:(NSString *)kABPersonAlternateBirthdayYearKey] integerValue];

  50. // 月份

  51. brithdayModel.month = [[brithdayDict valueForKey:(NSString *)kABPersonAlternateBirthdayMonthKey] integerValue];

  52. // 日期

  53. brithdayModel.day = [[brithdayDict valueForKey:(NSString *)kABPersonAlternateBirthdayDayKey] integerValue];

  54. // 是否闰月

  55. brithdayModel.leapMonth = [[brithdayDict valueForKey:(NSString *)kABPersonAlternateBirthdayIsLeapMonthKey] boolValue];

  56.  
  57. _alternateBirthday = brithdayModel;

  58. }

  59.  
  60.  
  61. /** 分类 */

  62. CFNumberRef kindType = ABRecordCopyValue(person, kABPersonKindProperty);

  63. _kind = (kindType == kABPersonKindOrganization) ? KindOrganization : KindPerson;

  64. if (kindType != NULL) CFRelease(kindType);

  65.  
  66. /** 头像 */

  67. if (ABPersonHasImageData(person) == true) {

  68. // 获得头像原图

  69. NSData *imageData = CFBridgingRelease(ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatOriginalSize));

  70. _headImage = [UIImage imageWithData:imageData];

  71. }

  72.  
  73. /** 电子邮件列表 */

  74. NSMutableArray *emailArray = [NSMutableArray array];

  75. ABMultiValueRef emailRef = ABRecordCopyValue(person, kABPersonEmailProperty);

  76. for (int i = 0; i < ABMultiValueGetCount(emailRef); i++)

  77. {

  78. ZMLabelStringModel *emailModel = [[ZMLabelStringModel alloc] init];

  79. // 标签

  80. emailModel.label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(emailRef, i));;

  81. // 邮箱地址

  82. emailModel.content = CFBridgingRelease(ABMultiValueCopyValueAtIndex(emailRef, i));

  83. // 添加到数组

  84. [emailArray addObject:emailModel];

  85. }

  86. if (emailRef) CFRelease(emailRef);

  87. _emails = emailArray;

  88.  
  89. /** 地址列表 */

  90. NSMutableArray *addressArray = [NSMutableArray array];

  91. ABMultiValueRef addressRef = ABRecordCopyValue(person, kABPersonAddressProperty);

  92. for(int i = 0; i < ABMultiValueGetCount(addressRef); i++)

  93. {

  94. ZMAddressModel *addressModel = [[ZMAddressModel alloc] init];

  95. // 标签

  96. addressModel.label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(addressRef, i));

  97. // 获取地址字典

  98. NSDictionary *addressDict = CFBridgingRelease(ABMultiValueCopyValueAtIndex(addressRef, i));

  99. if (!IsNullDictionary(addressDict)) {

  100. // 街道

  101. addressModel.street = [addressDict valueForKey:(NSString *)kABPersonAddressStreetKey];

  102. // 城市

  103. addressModel.city = [addressDict valueForKey:(NSString *)kABPersonAddressCityKey];

  104. // 省

  105. addressModel.state = [addressDict valueForKey:(NSString *)kABPersonAddressStateKey];

  106. // 邮政编码

  107. addressModel.zip = [addressDict valueForKey:(NSString *)kABPersonAddressZIPKey];

  108. // 国家

  109. addressModel.country = [addressDict valueForKey:(NSString *)kABPersonAddressCountryKey];

  110. // 国别码

  111. addressModel.countryCode = [addressDict valueForKey:(NSString *)kABPersonAddressCountryCodeKey];

  112. }

  113. // 添加到数组

  114. [addressArray addObject:addressModel];

  115. }

  116. if (addressRef) CFRelease(addressRef);

  117. _addresses = addressArray;

  118.  
  119. /** 日期列表 */

  120. NSMutableArray *dateArray = [NSMutableArray array];

  121. ABMultiValueRef dateRef = ABRecordCopyValue(person, kABPersonDateProperty);

  122. for (int i = 0; i < ABMultiValueGetCount(dateRef); i++)

  123. {

  124. ZMLabelDateModel *dateModel = [[ZMLabelDateModel alloc] init];

  125. // 标签

  126. dateModel.label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(dateRef, i));

  127. // 日期

  128. dateModel.date = CFBridgingRelease(ABMultiValueCopyValueAtIndex(dateRef, i));

  129. // 添加到数组

  130. [dateArray addObject:dateModel];

  131. }

  132. if (dateRef) CFRelease(dateRef);

  133. _dates = dateArray;

  134.  
  135. /** 电话列表 */

  136. NSMutableArray *phoneArray = [NSMutableArray array];

  137. ABMultiValueRef phoneRef = ABRecordCopyValue(person, kABPersonPhoneProperty);

  138. for (int i = 0; i < ABMultiValueGetCount(phoneRef); i++)

  139. {

  140. ZMLabelStringModel *phoneModel = [[ZMLabelStringModel alloc] init];

  141. // 标签

  142. phoneModel.label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phoneRef, i));

  143. // 电话号码

  144. phoneModel.content = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneRef, i));

  145. // 添加到数组

  146. [phoneArray addObject:phoneModel];

  147. }

  148. if (phoneRef) CFRelease(phoneRef);

  149. _phones = phoneArray;

  150.  
  151. /** 即时信息列表 */

  152. NSMutableArray *instantMessageArray = [NSMutableArray array];

  153. ABMultiValueRef instantMessageRef = ABRecordCopyValue(person, kABPersonInstantMessageProperty);

  154. for (int i = 1; i < ABMultiValueGetCount(instantMessageRef); i++)

  155. {

  156. ZMInstantMessageModel *instantMessageModel = [[ZMInstantMessageModel alloc] init];

  157. // 标签

  158. instantMessageModel.label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(instantMessageRef, i));

  159. // 获取即时信息字典

  160. NSDictionary *instantMessageDict = CFBridgingRelease(ABMultiValueCopyValueAtIndex(instantMessageRef, i));

  161. if (!IsNullDictionary(instantMessageDict)) {

  162. // 服务账号

  163. instantMessageModel.username = [instantMessageDict valueForKey:(NSString *)kABPersonInstantMessageUsernameKey];

  164. // 服务名称

  165. instantMessageModel.service = [instantMessageDict valueForKey:(NSString *)kABPersonInstantMessageServiceKey];

  166. }

  167. // 添加到数组

  168. [instantMessageArray addObject:instantMessageModel];

  169. }

  170. if (instantMessageRef) CFRelease(instantMessageRef);

  171. _instantMessages = instantMessageArray;

  172.  
  173. /** URL列表 */

  174. NSMutableArray *urlArray = [NSMutableArray array];

  175. ABMultiValueRef urlRef = ABRecordCopyValue(person, kABPersonURLProperty);

  176. for (int i = 0; i < ABMultiValueGetCount(urlRef); i++)

  177. {

  178. ZMLabelStringModel *urlModel = [[ZMLabelStringModel alloc] init];

  179. // 标签

  180. urlModel.label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(urlRef,i));

  181. // URL地址

  182. urlModel.content = CFBridgingRelease(ABMultiValueCopyValueAtIndex(urlRef,i));

  183. // 添加到数组

  184. [urlArray addObject:urlModel];

  185. }

  186. if (urlRef) CFRelease(urlRef);

  187. _urls = urlArray;

  188.  
  189. /** 关联人列表 */

  190. NSMutableArray *relatedNameArray = [NSMutableArray array];

  191. ABMultiValueRef relatedNameRef = ABRecordCopyValue(person, kABPersonRelatedNamesProperty);

  192. for (int i = 0; i < ABMultiValueGetCount(relatedNameRef); i++)

  193. {

  194. ZMLabelStringModel *relatedNameModel = [[ZMLabelStringModel alloc] init];

  195. // 标签

  196. relatedNameModel.label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(relatedNameRef, i));

  197. // 关联人姓名

  198. relatedNameModel.content = CFBridgingRelease(ABMultiValueCopyValueAtIndex(relatedNameRef, i));

  199. // 添加到数组

  200. [relatedNameArray addObject:relatedNameModel];

  201. }

  202. if (relatedNameRef) CFRelease(relatedNameRef);

  203. _relatedNames = relatedNameArray;

  204.  
  205. /** 社交资料列表 */

  206. NSMutableArray *socialProfileArray = [NSMutableArray array];

  207. ABMultiValueRef socialProfileRef = ABRecordCopyValue(person, kABPersonSocialProfileProperty);

  208. for (int i = 0 ; i < ABMultiValueGetCount(socialProfileRef); i++)

  209. {

  210. // 获取社交资料字典

  211. NSDictionary *socialProfileDict = CFBridgingRelease(ABMultiValueCopyValueAtIndex(socialProfileRef, i));

  212. if (!IsNullDictionary(socialProfileDict)) {

  213. ZMSocialProfileModel *socialProfileModel = [[ZMSocialProfileModel alloc] init];

  214. // 社交链接

  215. socialProfileModel.url = [socialProfileDict valueForKey:(NSString *)kABPersonSocialProfileURLKey];

  216. // 服务名称

  217. socialProfileModel.service = [socialProfileDict valueForKey:(NSString *)kABPersonSocialProfileServiceKey];

  218. // 社交服务账号

  219. socialProfileModel.username = [socialProfileDict valueForKey:(NSString *)kABPersonSocialProfileUsernameKey];

  220. // 用户标识

  221. socialProfileModel.userIdentifier = [socialProfileDict valueForKey:(NSString *)kABPersonSocialProfileUserIdentifierKey];

  222. // 添加到数组

  223. [socialProfileArray addObject:socialProfileModel];

  224. }

  225. }

  226. if (socialProfileRef) CFRelease(socialProfileRef);

  227. _socialProfiles = socialProfileArray;

  228. }

 

 

2. 生成联系所有数据

 

 

 
  1. /**

  2. 将Model数据赋值到ABRecordRef

  3.  
  4. @param person ABRecordRef对象

  5. @return 赋值结果

  6. */

  7. - (BOOL)updateToRecordRef:(ABRecordRef)person

  8. {

  9. // 空值处理

  10. if (person == NULL) {

  11. return NO;

  12. }

  13.  
  14. // 实例化CFErrorRef

  15. CFErrorRef errorRef = NULL;

  16.  
  17. /** 姓名相关 */

  18. ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFStringRef)(_firstName), &errorRef); //!< 名字.

  19. ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFStringRef)(_lastName), &errorRef); //!< 姓氏.

  20. ABRecordSetValue(person, kABPersonMiddleNameProperty, (__bridge CFStringRef)(_middleName), &errorRef);//!< 中间名.

  21. ABRecordSetValue(person, kABPersonPrefixProperty, (__bridge CFStringRef)(_prefix), &errorRef); //!< 前缀.

  22. ABRecordSetValue(person, kABPersonSuffixProperty, (__bridge CFStringRef)(_suffix), &errorRef); //!< 后缀.

  23. ABRecordSetValue(person, kABPersonNicknameProperty, (__bridge CFStringRef)(_nickname), &errorRef); //!< 昵称.

  24. ABRecordSetValue(person, kABPersonFirstNamePhoneticProperty, (__bridge CFStringRef)(_firstNamePhonetic), &errorRef); //!< 名字拼音或音标.

  25. ABRecordSetValue(person, kABPersonLastNamePhoneticProperty, (__bridge CFStringRef)(_lastNamePhonetic), &errorRef); //!< 姓氏拼音或音标.

  26. ABRecordSetValue(person, kABPersonMiddleNamePhoneticProperty, (__bridge CFStringRef)(_middleNamePhonetic), &errorRef);//!< 中间名拼音或音标.

  27.  
  28. /** 工作相关 */

  29. ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFStringRef)(_organization), &errorRef);//!< 公司名称.

  30. ABRecordSetValue(person, kABPersonDepartmentProperty, (__bridge CFStringRef)(_department), &errorRef); //!< 部门.

  31. ABRecordSetValue(person, kABPersonJobTitleProperty, (__bridge CFStringRef)(_jobTitle), &errorRef); //!< 职位.

  32.  
  33. /** 备注 */

  34. ABRecordSetValue(person, kABPersonNoteProperty, (__bridge CFStringRef)(_note), &errorRef); //!< 备注.

  35.  
  36. /** 日期相关 */

  37. ABRecordSetValue(person, kABPersonBirthdayProperty, (__bridge CFDateRef)(_birthday), &errorRef); //!< 生日.

  38. ABRecordSetValue(person, kABPersonCreationDateProperty, (__bridge CFDateRef)(_creationDate), &errorRef); //!< 创建时间.

  39. ABRecordSetValue(person, kABPersonModificationDateProperty, (__bridge CFDateRef)(_modificationDate), &errorRef);//!< 最近修改时间.

  40.  
  41. /** 农历生日 */

  42. if (_alternateBirthday)

  43. {

  44. NSMutableDictionary *brithdayDict = [NSMutableDictionary dictionary];

  45. // 标志

  46. brithdayDict[(NSString *)kABPersonAlternateBirthdayCalendarIdentifierKey] = _alternateBirthday.calendar;

  47. // 纪元

  48. brithdayDict[(NSString *)kABPersonAlternateBirthdayEraKey] = @(_alternateBirthday.era);

  49. // 年份

  50. brithdayDict[(NSString *)kABPersonAlternateBirthdayYearKey] = @(_alternateBirthday.year);

  51. // 月份

  52. brithdayDict[(NSString *)kABPersonAlternateBirthdayMonthKey] = @(_alternateBirthday.month);

  53. // 日期

  54. brithdayDict[(NSString *)kABPersonAlternateBirthdayDayKey] = @(_alternateBirthday.day);

  55. // 是否闰月

  56. brithdayDict[(NSString *)kABPersonAlternateBirthdayIsLeapMonthKey] = @(_alternateBirthday.leapMonth);

  57.  
  58. ABRecordSetValue(person, kABPersonAlternateBirthdayProperty, (__bridge CFDictionaryRef)(brithdayDict), &errorRef);

  59. }

  60.  
  61. /** 分类 */

  62. CFNumberRef personKind = (_kind == KindOrganization) ? kABPersonKindOrganization : kABPersonKindPerson;

  63. ABRecordSetValue(person, kABPersonKindProperty, personKind, &errorRef);

  64.  
  65. /** 头像 */

  66. if (_headImage) {

  67. NSData *imageData = UIImagePNGRepresentation(_headImage);

  68. ABPersonSetImageData(person, (__bridge CFDataRef)(imageData),&errorRef);

  69. }

  70.  
  71. /** 电子邮件列表 */

  72. ABMultiValueRef emailRef = ABMultiValueCreateMutable(kABMultiStringPropertyType);

  73. for (ZMLabelStringModel *emailModel in _emails) {

  74. ABMultiValueAddValueAndLabel(emailRef, (__bridge CFStringRef)(emailModel.content), (__bridge CFStringRef)(emailModel.label), NULL);

  75. }

  76. ABRecordSetValue(person, kABPersonEmailProperty, emailRef, &errorRef);

  77. if (emailRef) CFRelease(emailRef);

  78.  
  79. /** 地址列表 */

  80. ABMultiValueRef addressRef = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

  81. for (ZMAddressModel *addressModel in _addresses)

  82. {

  83. NSMutableDictionary *addressDict = [NSMutableDictionary dictionary];

  84. // 街道

  85. addressDict[(NSString *)kABPersonAddressStreetKey] = addressModel.street;

  86. // 城市

  87. addressDict[(NSString *)kABPersonAddressCityKey] = addressModel.city;

  88. // 省

  89. addressDict[(NSString *)kABPersonAddressStateKey] = addressModel.state;

  90. // 邮政编码

  91. addressDict[(NSString *)kABPersonAddressZIPKey] = addressModel.zip;

  92. // 国家

  93. addressDict[(NSString *)kABPersonAddressCountryKey] = addressModel.country;

  94. // 国别码

  95. addressDict[(NSString *)kABPersonAddressCountryCodeKey] = addressModel.countryCode;

  96.  
  97. ABMultiValueAddValueAndLabel(addressRef, (__bridge CFDictionaryRef)(addressDict), (__bridge CFStringRef)addressModel.label, NULL);

  98. }

  99. ABRecordSetValue(person, kABPersonAddressProperty, addressRef, &errorRef);

  100. if (addressRef) CFRelease(addressRef);

  101.  
  102. /** 日期列表 */

  103. ABMultiValueRef dateRef = ABMultiValueCreateMutable(kABMultiDateTimePropertyType);

  104. for (ZMLabelDateModel *dateModel in _dates) {

  105. ABMultiValueAddValueAndLabel(dateRef, (__bridge CFDateRef)(dateModel.date), (__bridge CFStringRef)(dateModel.label), NULL);

  106. }

  107. ABRecordSetValue(person, kABPersonDateProperty, dateRef, &errorRef);

  108. if (dateRef) CFRelease(dateRef);

  109.  
  110. /** 电话列表 */

  111. ABMultiValueRef phoneRef = ABMultiValueCreateMutable(kABMultiStringPropertyType);

  112. for (ZMLabelStringModel *phoneModel in _phones) {

  113. ABMultiValueAddValueAndLabel(phoneRef, (__bridge CFStringRef)(phoneModel.content), (__bridge CFStringRef)(phoneModel.label), NULL);

  114. }

  115. ABRecordSetValue(person, kABPersonPhoneProperty, phoneRef, &errorRef);

  116. if (phoneRef) CFRelease(phoneRef);

  117.  
  118. /** 即时信息列表 */

  119. ABMultiValueRef instantMessageRef = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

  120. for (ZMInstantMessageModel *instantMessageModel in _instantMessages)

  121. {

  122. NSMutableDictionary *instantMessageDict = [NSMutableDictionary dictionary];

  123. // 服务账号

  124. instantMessageDict[(NSString *)kABPersonInstantMessageUsernameKey] = instantMessageModel.username;

  125. // 服务名称

  126. instantMessageDict[(NSString *)kABPersonInstantMessageServiceKey] = instantMessageModel.service;

  127.  
  128. ABMultiValueAddValueAndLabel(instantMessageRef, (__bridge CFDictionaryRef)(instantMessageDict), (__bridge CFStringRef)instantMessageModel.label, NULL);

  129. }

  130. ABRecordSetValue(person, kABPersonInstantMessageProperty, instantMessageRef, &errorRef);

  131. if (instantMessageRef) CFRelease(instantMessageRef);

  132.  
  133. /** URL列表 */

  134. ABMultiValueRef urlRef = ABMultiValueCreateMutable(kABMultiStringPropertyType);

  135. for (ZMLabelStringModel *urlModel in _urls) {

  136. ABMultiValueAddValueAndLabel(urlRef, (__bridge CFStringRef)(urlModel.content), (__bridge CFStringRef)(urlModel.label), NULL);

  137. }

  138. ABRecordSetValue(person, kABPersonURLProperty, urlRef, &errorRef);

  139. if (urlRef) CFRelease(urlRef);

  140.  
  141. /** 关联人列表 */

  142. ABMultiValueRef relatedNameRef = ABMultiValueCreateMutable(kABMultiStringPropertyType);

  143. for (ZMLabelStringModel *relatedNameModel in _relatedNames) {

  144. ABMultiValueAddValueAndLabel(relatedNameRef, (__bridge CFStringRef)(relatedNameModel.content), (__bridge CFStringRef)(relatedNameModel.label), NULL);

  145. }

  146. ABRecordSetValue(person, kABPersonRelatedNamesProperty, relatedNameRef, &errorRef);

  147. if (relatedNameRef) CFRelease(relatedNameRef);

  148.  
  149. /** 社交资料列表 */

  150. ABMultiValueRef socialProfileRef = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);

  151. for (ZMSocialProfileModel *socialProfileModel in _socialProfiles)

  152. {

  153. NSMutableDictionary *socialProfileDict = [NSMutableDictionary dictionary];

  154. // 社交链接

  155. socialProfileDict[(NSString *)kABPersonSocialProfileURLKey] = socialProfileModel.url;

  156. // 服务名称

  157. socialProfileDict[(NSString *)kABPersonSocialProfileServiceKey] = socialProfileModel.service;

  158. // 社交服务账号

  159. socialProfileDict[(NSString *)kABPersonSocialProfileUsernameKey] = socialProfileModel.username;

  160. // 用户标识

  161. socialProfileDict[(NSString *)kABPersonSocialProfileUserIdentifierKey] = socialProfileModel.userIdentifier;

  162.  
  163. ABMultiValueAddValueAndLabel(socialProfileRef, (__bridge CFDictionaryRef)(socialProfileDict), NULL, NULL);

  164. }

  165. ABRecordSetValue(person, kABPersonSocialProfileProperty, socialProfileRef, &errorRef);

  166. if (socialProfileRef) CFRelease(socialProfileRef);

  167.  
  168. // 错误消息处理

  169. if (errorRef) {

  170. NSLog(@"ZMPersonModel to RecordRef error:%@", errorRef);

  171. CFRelease(errorRef);

  172. return NO;

  173. }

  174.  
  175. return YES;

  176. }

 

 

五.  通讯录改变监听

 

1. 注册通讯录变动监听

 

 
  1. // 注册通讯录变动的回调

  2. ABAddressBookRegisterExternalChangeCallback(_addressBook, addressBookChangeCallBack, (__bridge_retained void *)(self));

 

 

2. 实现通讯录变动监听

 

 

 
  1. /**

  2. 通讯录联系人变动的回调

  3.  
  4. @param addressBook 注册的addressBook

  5. @param info 变动之后进行的回调方法

  6. @param context 传参,这里是将自己作为参数传到方法中

  7. */

  8. void addressBookChangeCallBack(ABAddressBookRef addressBook, CFDictionaryRef info, void *context)

  9. {

  10. // 清除缓存,重置addressBook

  11. ABAddressBookRevert(addressBook);

  12.  
  13. }

 

 

3. 移除通讯录变动监听

 

 

 
  1. // 移除监听通讯录变动的回调

  2. ABAddressBookUnregisterExternalChangeCallback(_addressBook, addressBookChangeCallBack, (__bridge void *)(self));

 

 

六. Demo展示

 

1. Demo效果图

 

2. Demo下载: https://github.com/zeng-zhiming/ZMAddressBook

 

 

 

原文地址:http://blog.csdn.net/zeng_zhiming/article/details/70141316

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(功能模块,一天一读,ios,objective-c)