ios9 contacts联系人

2017-08-29  本文已影响24人  我的名字就这么长

1、引入头文件#import<Contacts/Contacts.h>

2、- (void)viewDidLoad {

[super viewDidLoad];

//2.添加联系人

UIButton *addContacts = [UIButton buttonWithType:UIButtonTypeCustom];

addContacts.frame = CGRectMake(100, 150, 100, 50);

[addContacts setTitle:@"添加联系人" forState:UIControlStateNormal];

addContacts.backgroundColor = [UIColor greenColor];

[addContacts setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[addContacts addTarget:self action:@selector(creatContact) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:addContacts];

UIButton *deleteContacts = [UIButton buttonWithType:UIButtonTypeCustom];

deleteContacts.frame = CGRectMake(100, 300, 100, 50);

[deleteContacts setTitle:@"删除联系人" forState:UIControlStateNormal];

deleteContacts.backgroundColor = [UIColor greenColor];

[deleteContacts setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[deleteContacts addTarget:self action:@selector(clearContacts) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:deleteContacts];

}

3、创建联系人

-(void)creatContact

{

NSArray* array = @[@"18888888888",@"18888888887"];

for (NSString*phoneString in array) {

CNMutableContact *contact = [[CNMutableContact alloc] init]; // 第一次运行的时候,会获取通讯录的授权(对通讯录进行操作,有权限设置)

// 1、添加姓名(姓+名)

contact.givenName = phoneString;

contact.familyName = @"zzzzzz";

//    contact.nickname = @"hahahah"; // 昵称

//    contact.nameSuffix = @"nameSuffix"; // 名字后缀

//    contact.namePrefix = @"namePrefix"; // 前字后缀

//    contact.previousFamilyName = @"previousFamilyName"; // 之前的familyName

// 2、添加职位相关

//    contact.organizationName = @"公司名称";

//    contact.departmentName = @"开发部门";

//    contact.jobTitle = @"工程师";

// 3、这一部分内容会显示在联系人名字的下面,phoneticFamilyName属性设置的话,会影响联系人列表界面的排序

//    contact.phoneticGivenName = @"GivenName";

//    contact.phoneticFamilyName = @"FamilyName";

//    contact.phoneticMiddleName = @"MiddleName";

//    // 4、备注

//    contact.note = @"同事";

// 5、头像

//    contact.imageData = UIImagePNGRepresentation([UIImage imageNamed:@"1"]);

//    // 6、添加生日

//    NSDateComponents *birthday = [[NSDateComponents alloc] init];

//    birthday.year = 1990;

//    birthday.month = 6;

//    birthday.day = 6;

//    contact.birthday = birthday;

//    // 7、添加邮箱

//    CNLabeledValue *homeEmail = [CNLabeledValue labeledValueWithLabel:CNLabelEmailiCloud value:@"bvbdsmv@icloud.com"];

//    //    CNLabeledValue *workEmail = [CNLabeledValue labeledValueWithLabel:CNLabelWork value:@"11111888888"];

//    //    CNLabeledValue *iCloudEmail = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:@"34454554"];

//    //    CNLabeledValue *otherEmail = [CNLabeledValue labeledValueWithLabel:CNLabelOther value:@"6565448"];

//    contact.emailAddresses = @[homeEmail];

// 8、添加电话

CNLabeledValue *homePhone = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberiPhone value:[CNPhoneNumber phoneNumberWithStringValue:phoneString]];

contact.phoneNumbers = @[homePhone];

//    // 9、添加urlAddresses,

//    CNLabeledValue *homeurl = [CNLabeledValue labeledValueWithLabel:CNLabelURLAddressHomePage value:@"http://baidu.com"];

//    contact.urlAddresses = @[homeurl];

//

//    // 10、添加邮政地址

//    CNMutablePostalAddress *postal = [[CNMutablePostalAddress alloc] init];

//    postal.city = @"北京";

//    postal.country =  @"中国";

//    CNLabeledValue *homePostal = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:postal];

//    contact.postalAddresses = @[homePostal];

// 获取通讯录操作请求对象

CNSaveRequest *request = [[CNSaveRequest alloc] init];

[request addContact:contact toContainerWithIdentifier:nil]; // 添加联系人操作(同一个联系人可以重复添加)

// 获取通讯录

CNContactStore *store = [[CNContactStore alloc] init];

// 保存联系人

[store executeSaveRequest:request error:nil]; // 通讯录有变化之后,还可以监听是否改变(CNContactStoreDidChangeNotification)

}

}

4、删除好友

- (void)clearContacts

   // 创建通讯录    CNContactStore *contactStore = [[CNContactStore alloc] init];     

   NSError *error;  

  NSPredicate *predicate = [CNContact predicateForContactsMatchingName:@"zzzzzz"]; // 按照谓词进行查询,predicate必须利用CNContact的predicate分类创建,方法都在中

NSArray *keys = @[CNContactGivenNameKey,CNContactFamilyNameKey]; // 所查询的联系人中的信息按照keys中的字断进行提取

NSArray *allContacts = [contactStore unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error]; // allContacts数组中存储查询的联系人

NSLog(@"count==%lu",(unsigned long)allContacts.count);

for (CNContact *contact in allContacts) {

NSLog(@"==%@",contact);

CNMutableContact * newContact = [contact mutableCopy];

CNSaveRequest* request = [[CNSaveRequest alloc]init];

newContact.givenName = contact.givenName;

newContact.familyName = contact.familyName;

//必须先更新-再删除-最后保存

[request updateContact:newContact];

[request deleteContact:newContact];

[contactStore executeSaveRequest:request error:nil];

}

}

上一篇 下一篇

猜你喜欢

热点阅读