首先你要引入兩個庫 在
![Upload 屏幕快照 2017-09-12 上午11.09.04.png failed. Please try again.]
引入之后
引入
import <ContactsUI/CNContactViewController.h>
import <ContactsUI/CNContactPickerViewController.h>
在遵循這個CNContactViewControllerDelegate,CNContactPickerDelegate,
代理
這里的代碼可以直接復(fù)制直接使用很是方便
- (void)saveNewContact{
//1.創(chuàng)建Contact對象,必須是可變的
CNMutableContact *contact = [[CNMutableContact alloc] init];
//2.為contact賦值,setValue4Contact中會給出常用值的對應(yīng)關(guān)系
[self setValue4Contact:contact existContect:NO];
//3.創(chuàng)建新建好友頁面
CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:contact];
//代理內(nèi)容根據(jù)自己需要實現(xiàn)
controller.delegate = self;
//4.跳轉(zhuǎn)
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:controller];
[self presentViewController:navigation animated:YES completion:^{
}];
}
//保存現(xiàn)有聯(lián)系人實現(xiàn)
- (void)saveExistContact{
//1.跳轉(zhuǎn)到聯(lián)系人選擇頁面,注意這里沒有使用UINavigationController
CNContactPickerViewController *controller = [[CNContactPickerViewController alloc] init];
controller.delegate = self;
[self presentViewController:controller animated:YES completion:^{
}];
}
- (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(nullable CNContact *)contact{
[viewController dismissViewControllerAnimated:YES completion:^{
}];
}
#pragma mark - CNContactPickerDelegate
//2.實現(xiàn)點選的代理,其他代理方法根據(jù)自己需求實現(xiàn)
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
[picker dismissViewControllerAnimated:YES completion:^{
//3.copy一份可寫的Contact對象,不要嘗試alloc一類,mutableCopy獨此一家
CNMutableContact *c = [contact mutableCopy];
//4.為contact賦值
[self setValue4Contact:c existContect:YES];
//5.跳轉(zhuǎn)到新建聯(lián)系人頁面
CNContactViewController *controller = [CNContactViewController viewControllerForNewContact:c];
controller.delegate = self;
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:controller];
[self presentViewController:navigation animated:YES completion:^{
}];
}];
}
//設(shè)置要保存的contact對象
- (void)setValue4Contact:(CNMutableContact *)contact existContect:(BOOL)exist{
if (!exist) {
//名字和頭像
contact.namePrefix = @"3333";
contact.nickname = @"oriccheng";
// UIImage *logo = [UIImage imageNamed:@"..."];
// NSData *dataRef = UIImagePNGRepresentation(logo);
// contact.imageData = dataRef;
}
//電話,每一個CNLabeledValue都是有講究的,如何批評,可以在頭文件里面查找,這里是給出幾個常用的
CNLabeledValue *phoneNumber = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberMobile value:[CNPhoneNumber phoneNumberWithStringValue:@"18888888888"]];
if (!exist) {
contact.phoneNumbers = @[phoneNumber];
}
//現(xiàn)有聯(lián)系人情況
else{
if ([contact.phoneNumbers count] >0) {
NSMutableArray *phoneNumbers = [[NSMutableArray alloc] initWithArray:contact.phoneNumbers];
[phoneNumbers addObject:phoneNumber];
contact.phoneNumbers = phoneNumbers;
}else{
contact.phoneNumbers = @[phoneNumber];
}
}
//網(wǎng)址:CNLabeledValue *url = [CNLabeledValue labeledValueWithLabel:@"" value:@""];
//郵箱:CNLabeledValue *mail = [CNLabeledValue labeledValueWithLabel:CNLabelWork value:self.poiData4Save.mail];
//特別說一個地址,PostalAddress對應(yīng)的才是地址
CNMutablePostalAddress *address = [[CNMutablePostalAddress alloc] init];
address.state = @"遼寧省";
address.city = @"沈陽市";
address.postalCode = @"111111";
//外國人好像都不強調(diào)區(qū)的概念,和具體地址拼到一起
address.street = @"沈河區(qū)惠工街10號";
//生成的上面地址的CNLabeledValue,其中可以設(shè)置類型CNLabelWork等等
CNLabeledValue *addressLabel = [CNLabeledValue labeledValueWithLabel:CNLabelWork value:address];
if (!exist) {
contact.postalAddresses = @[addressLabel];
}else{
if ([contact.postalAddresses count] >0) {
NSMutableArray *addresses = [[NSMutableArray alloc] initWithArray:contact.postalAddresses];
[addresses addObject:addressLabel];
contact.postalAddresses = addresses;
}else{
contact.postalAddresses = @[addressLabel];
}
}
}