【链接】CNContact对通讯录的基本使用(第二篇)
http://www.cnblogs.com/lantu1989/p/5469346.html
**
* 注意:iOS9才有能使用
* 首先在工程里导入ContactsUI.framework和Contacts.framework两个框架
*
*
* 源代码的链接地址
* 链接: http://pan.baidu.com/s/1jHDsvGU 密码: 7tfj
*/
#import "RootViewController.h"
#import
#import
@interface RootViewController ()
{
UITableView *_contactTableView;
NSMutableArray *data;
}
@end
@implementation RootViewController
- (void)loadView{
[super loadView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 120, 60);
[button setTitle:@"显示联系人" forState:0];
[button setTitleColor:[UIColor cyanColor] forState:0];
[button addTarget:self action:@selector(showContact:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)viewDidLoad {
[super viewDidLoad];
// CNMutableContact *contact = [self initializeContact];
//添加联系人
// [self addContact:contact];
// 查询
// NSArray *arr = [self queryContactWithName:@"雨路"];
//将不可变的CNContact转成CNMutableContact,便可执行以下操作
// CNMutableContact *contact = [arr[1] mutableCopy];
// 更新
// contact.givenName = @"LF";
// [self updateContact:contact];
// 删除
// [self deleteContact:contact];
//添加群组
// [self addGroupWithName:@"friend"];
// NSArray *array = [self queryGroup];
// NSLog(@"==%@",array);
// 删除group操作
// CNMutableGroup *group = [array[3] mutableCopy];
// [self deleteWithGroup:group];
// 更新group
// CNMutableGroup *group = [array[3] mutableCopy];
// group.name = @"i need you";
// [self updateGroup:group];
// 向group添加成员
// NSArray *results = [self queryContactWithName:@"甘"];
// CNGroup *group = array[3];
// [self addMemberWithContact:results[0] toGroup:group];
// 在group里删除成员
// NSArray *results = [self queryContactWithName:@"甘"];
// CNGroup *group = array[3];
// [self deleteMemberWithContact:results[0] toGroup:group];
}
/**
* 在group里删除成员
*
* @param contact 被删除的联系人
* @param group 在该group里删除联系人
*/
- (void)deleteMemberWithContact:(CNContact *)contact toGroup:(CNGroup *)group{
CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
[saveRequest removeMember:contact fromGroup:group];
// 写入
CNContactStore *store = [[CNContactStore alloc] init];
[store executeSaveRequest:saveRequest error:nil];
}
/**
* 向group添加成员
*
* @param contact 被添加的联系人
* @param group 添加到该group
*/
- (void)addMemberWithContact:(CNContact *)contact toGroup:(CNGroup *)group{
CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
[saveRequest addMember:contact toGroup:group];
// 写入
CNContactStore *store = [[CNContactStore alloc] init];
[store executeSaveRequest:saveRequest error:nil];
}
/**
* 更新group
*
* @param group 被更新的group
*/
- (void)updateGroup:(CNMutableGroup *)group{
CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
[saveRequest updateGroup:group];
// 写入
CNContactStore *store = [[CNContactStore alloc] init];
[store executeSaveRequest:saveRequest error:nil];
}
/**
* 删除group操作
*
* @param group 被删除的group
*/
- (void)deleteWithGroup:(CNMutableGroup *)group{
CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
[saveRequest deleteGroup:group];
// 写入
CNContactStore *store = [[CNContactStore alloc] init];
[store executeSaveRequest:saveRequest error:nil];
}
/**
* 查询
*
* @return 返回数组
*/
- (NSArray *)queryGroup{
CNContactStore *store = [[CNContactStore alloc] init];
// 查询所有的group(predicate参数为空时会查询所有的group)
NSArray *arr = [store groupsMatchingPredicate:nil error:nil];
return arr;
}
/**
* 添加群组
*
* @param name 群组的名称
*/
- (void)addGroupWithName:(NSString *)name{
CNMutableGroup *group = [[CNMutableGroup alloc] init];
group.name = name;
CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
[saveRequest addGroup:group toContainerWithIdentifier:nil];
// 写入
CNContactStore *store = [[CNContactStore alloc] init];
[store executeSaveRequest:saveRequest error:nil];
}
/**
* 查询操作
*
* @return 返回数组
*/
- (NSArray *)queryContactWithName:(NSString *)name{
CNContactStore *store = [[CNContactStore alloc] init];
// 检索条件
NSPredicate *predicate = [CNContact predicateForContactsMatchingName:name];
// 提取数据 (keysToFetch:@[CNContactGivenNameKey]是设置提取联系人的哪些数据)
NSArray *contact = [store unifiedContactsMatchingPredicate:predicate keysToFetch:@[CNContactGivenNameKey] error:nil];
return contact;
}
/**
* 更新联系人
*
* @param contact 被更新的联系人
*/
- (void)updateContact:(CNMutableContact *)contact{
// 创建联系人请求
CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
[saveRequest updateContact:contact];
// 重新写入
CNContactStore *store = [[CNContactStore alloc] init];
[store executeSaveRequest:saveRequest error:nil];
}
/**
* 删除联系人
*
* @param contact 被删除的联系人
*/
- (void)deleteContact:(CNMutableContact *)contact{
// 创建联系人请求
CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
[saveRequest deleteContact:contact];
// 写入操作
CNContactStore *store = [[CNContactStore alloc] init];
[store executeSaveRequest:saveRequest error:nil];
}
/**
* 添加联系人
*
* @param contact 联系人
*/
- (void)addContact:(CNMutableContact *)contact{
// 创建联系人请求
CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];
[saveRequest addContact:contact toContainerWithIdentifier:nil];
// 写入联系人
CNContactStore *store = [[CNContactStore alloc] init];
[store executeSaveRequest:saveRequest error:nil];
}
/**
* 设置联系人的基本属性
*
* @return 返回联系人的对象
*/
- (CNMutableContact *)initializeContact{
// 创建联系人对象
CNMutableContact *contact = [[CNMutableContact alloc] init];
// 设置联系人的头像
contact.imageData = UIImagePNGRepresentation([UIImage imageNamed:@"animal"]);
// 设置联系人姓名
contact.givenName = @"雨路";
// 设置姓氏
contact.familyName = @"甘";
// 设置联系人邮箱
CNLabeledValue *homeEmail = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:@"12345qq.com"];
CNLabeledValue *workEmail = [CNLabeledValue labeledValueWithLabel:CNLabelWork value:@"[email protected]"];
CNLabeledValue *otherEmail = [CNLabeledValue labeledValueWithLabel:CNLabelOther value:@"lanaj.com"];
contact.emailAddresses = @[homeEmail,workEmail,otherEmail];
// 设置机构名
contact.organizationName = @"互联网";
// 设置部门
contact.departmentName = @"物流";
// 设置工作的名称
contact.jobTitle = @"ios";
// 设置社会的简述
CNSocialProfile *profile = [[CNSocialProfile alloc] initWithUrlString:@"12306.cn" username:@"lily" userIdentifier:nil service:@"IT行业"];
CNLabeledValue *socialProfile = [CNLabeledValue labeledValueWithLabel:CNSocialProfileServiceGameCenter value:profile];
contact.socialProfiles = @[socialProfile];
// 设置电话号码
CNPhoneNumber *mobileNumber = [[CNPhoneNumber alloc] initWithStringValue:@"15201596724"];
CNLabeledValue *mobilePhone = [[CNLabeledValue alloc] initWithLabel:CNLabelPhoneNumberMobile value:mobileNumber];
contact.phoneNumbers = @[mobilePhone];
// 设置与联系人的关系
CNContactRelation *friend = [[CNContactRelation alloc] initWithName:@"好朋友"];
CNLabeledValue *relation = [CNLabeledValue labeledValueWithLabel:CNLabelContactRelationFriend value:friend];
contact.contactRelations = @[relation];
// 设置生日
NSDateComponents *birthday = [[NSDateComponents alloc] init];
birthday.day = 6;
birthday.month = 5;
birthday.year = 2000;
contact.birthday = birthday;
return contact;
}
/**
* 显示联系人
*/
- (void)showContact:(UIButton *)sender{
// 初始化CNContactPickerViewController
CNContactPickerViewController *contactPickerViewController = [[CNContactPickerViewController alloc] init];
// 设置代理
contactPickerViewController.delegate = self;
// 显示联系人窗口视图
[self presentViewController:contactPickerViewController animated:YES completion:nil];
}
#pragma mark -- CNContactPickerDelegate 代理 --
/**
* 点击联系人控制器的Cancel按钮执行该方法
*
* @param picker 联系人控制器
*/
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker{
NSLog(@"取消");
}
/**
* 选中联系人时执行该方法
*
* @param picker 联系人控制器
* @param contact 联系人
*/
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
NSLog(@"联系人的资料:%@",contact);
[self dismissViewControllerAnimated:YES completion:nil];
//显示联系人详细页面
CNContactViewController *contactVC = [CNContactViewController viewControllerForContact:contact];
[self presentViewController:contactVC animated:YES completion:nil];
}
@end