IOS9中联系人对象的使用及增删改查操作的函数封装

之前克服重重困难把IOS9中新的类联系人搞明白了,现在把增删改查封装成了函数,如下:

  1 //
  2 //  ViewController.m
  3 //  IOS9中联系人CNContact的使用
  4 //
  5 //  Created by mac on 16/5/6.
  6 //  Copyright © 2016年 mzw. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 
 11 //导入IOS9联系人模型
 12 @import Contacts;
 13 
 14 @interface ViewController ()
 15 
 16 @end
 17 
 18 @implementation ViewController
 19 
 20 - (void)viewDidLoad {
 21     [super viewDidLoad];
 22     
 23     [self getContactList];
 24     [self addOneContactToContactList];
 25     [self deleteOneContactFromContactList];
 26     [self updateOneContectToContactList];
 27     
 28 }
 29 /**
 30  *  读取联系人通讯录
 31  */
 32 -(NSMutableArray*)getContactList{
 33     NSMutableArray *array = [NSMutableArray array];
 34     //     1.获取授权状态
 35     CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
 36     //      2.判断授权状态,如果不是已经授权,则直接返回
 37     if (status != CNAuthorizationStatusAuthorized) {
 38         return nil;
 39     }
 40     
 41     // 3.创建通信录对象
 42     CNContactStore *contactStore = [[CNContactStore alloc] init];
 43     
 44     // 4.创建获取通信录的请求对象
 45     // 4.1.拿到所有打算获取的属性对应的key
 46     NSArray *keys = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey,CNContactImageDataKey];
 47     
 48     // 4.2.创建CNContactFetchRequest对象
 49     CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
 50     
 51     // 5.遍历所有的联系人
 52     [contactStore enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
 53         [array addObject:contact];
 54         }
 55     ];
 56 
 57     return array;
 58     
 59 }
 60 
 61 
 62 
 63 /**
 64  *  删除通讯录中的一位联系人
 65  */
 66 -(void)deleteOneContactFromContactList{
 67     
 68         CNContactStore * store = [[CNContactStore alloc]init];
 69         //检索条件,检索所有名字中有zhang的联系人
 70         NSPredicate * predicate = [CNContact predicateForContactsMatchingName:@"W"];
 71         //提取数据
 72         NSArray * contacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:@[CNContactGivenNameKey] error:nil];
 73         CNMutableContact *contact1 = [contacts objectAtIndex:0];
 74     
 75     
 76        //初始化方法
 77         CNSaveRequest * saveRequest = [[CNSaveRequest alloc]init];
 78     
 79         //删除联系人(不行)
 80         [saveRequest deleteContact:contact1];
 81 
 82 }
 83 
 84 
 85 
 86 /**
 87  *  修改通讯录中的一位联系人
 88  *  先根据条件生成一个谓词,根据谓词读到联系人,并对联系人进行update
 89  */
 90 -(void)updateOneContectToContactList{
 91 
 92         CNContactStore * store = [[CNContactStore alloc]init];
 93         //检索条件,检索所有名字中有zhang的联系人
 94         NSPredicate * predicate = [CNContact predicateForContactsMatchingName:@""];
 95         //提取数据,要修改的必需先提取出来,放在keysToFetch中提取
 96         NSArray * contacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:@[CNContactGivenNameKey] error:nil];
 97     
 98         CNMutableContact * contact1 = [[contacts objectAtIndex:0] mutableCopy];
 99         contact1.givenName = @"heh";
100     
101         CNSaveRequest * saveRequest = [[CNSaveRequest alloc]init];
102     
103         [saveRequest updateContact:contact1];
104         [store executeSaveRequest:saveRequest error:nil];
105 }
106 
107 
108 /**
109  *  添加一位联系人到通讯录
110  */
111 //
112 -(void)addOneContactToContactList{
113     
114         CNMutableContact * contact = [[CNMutableContact alloc]init];
115         contact.imageData = UIImagePNGRepresentation([UIImage imageNamed:@"22"]);
116         //设置名字
117         contact.givenName = @"三强";
118         //设置姓氏
119         contact.familyName = @"";
120         CNLabeledValue *homeEmail = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:@"316045346@qq.com"];
121         CNLabeledValue *workEmail =[CNLabeledValue labeledValueWithLabel:CNLabelWork value:@"316045346@qq.com"];
122         contact.emailAddresses = @[homeEmail,workEmail];
123         //家庭
124         CONTACTS_EXTERN NSString * const CNLabelHome                             NS_AVAILABLE(10_11, 9_0);
125         //工作
126         CONTACTS_EXTERN NSString * const CNLabelWork                             NS_AVAILABLE(10_11, 9_0);
127         //其他
128         CONTACTS_EXTERN NSString * const CNLabelOther                            NS_AVAILABLE(10_11, 9_0);
129     
130         // 邮箱地址
131         CONTACTS_EXTERN NSString * const CNLabelEmailiCloud                      NS_AVAILABLE(10_11, 9_0);
132     
133         // url地址
134         CONTACTS_EXTERN NSString * const CNLabelURLAddressHomePage               NS_AVAILABLE(10_11, 9_0);
135     
136         // 日期
137         CONTACTS_EXTERN NSString * const CNLabelDateAnniversary                  NS_AVAILABLE(10_11, 9_0);
138         contact.phoneNumbers = @[[CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberiPhone value:[CNPhoneNumber phoneNumberWithStringValue:@"12344312321"]]];
139         CNMutablePostalAddress * homeAdress = [[CNMutablePostalAddress alloc]init];
140         homeAdress.street = @"贝克街";
141         homeAdress.city = @"伦敦";
142         homeAdress.state = @"英国";
143         homeAdress.postalCode = @"221B";
144         contact.postalAddresses = @[[CNLabeledValue labeledValueWithLabel:CNLabelHome value:homeAdress]];
145         NSDateComponents * birthday = [[NSDateComponents  alloc]init];
146         birthday.day=7;
147         birthday.month=5;
148         birthday.year=1992;
149         contact.birthday=birthday;
150     
151         //    //初始化方法
152         CNSaveRequest * saveRequest = [[CNSaveRequest alloc]init];
153         //    添加联系人(可以)
154         [saveRequest addContact:contact toContainerWithIdentifier:nil];
155         //    写入
156         CNContactStore * store = [[CNContactStore alloc]init];
157         [store executeSaveRequest:saveRequest error:nil];
158 
159 }
160 
161 
162 @end
原文地址:https://www.cnblogs.com/jiwangbujiu/p/5469185.html