/**
* 注意:iOS9才有能使用
* 首先在工程里导入ContactsUI.framework和Contacts.framework两个框架
*
*
* 源代码的链接地址
* 链接: http://pan.baidu.com/s/1jHDsvGU 密码: 7tfj
*/
#import "RootViewController.h"
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
@interface RootViewController ()<CNContactPickerDelegate>
{
UITableView *_contactTableView;
NSMutableArray *data;
}
@end @implementation RootViewController - (void)loadView{
[super loadView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(, , , );
[button setTitle:@"显示联系人" forState:];
[button setTitleColor:[UIColor cyanColor] forState:];
[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:@"32467@sina.cn"];
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:@""];
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 = ;
birthday.month = ;
birthday.year = ;
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

CNContact对通讯录的基本使用(第二篇)的更多相关文章

  1. [ 高并发]Java高并发编程系列第二篇--线程同步

    高并发,听起来高大上的一个词汇,在身处于互联网潮的社会大趋势下,高并发赋予了更多的传奇色彩.首先,我们可以看到很多招聘中,会提到有高并发项目者优先.高并发,意味着,你的前雇主,有很大的业务层面的需求, ...

  2. 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)

    从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...

  3. (转)从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)

    原文地址:  http://www.cnblogs.com/lyhabc/p/4682028.html 这一篇是从0开始搭建SQL Server AlwaysOn 的第二篇,主要讲述如何搭建故障转移集 ...

  4. 深入理解javascript对象系列第二篇——属性操作

    × 目录 [1]查询 [2]设置 [3]删除[4]继承 前面的话 对于对象来说,属性操作是绕不开的话题.类似于“增删改查”的基本操作,属性操作分为属性查询.属性设置.属性删除,还包括属性继承.本文是对 ...

  5. 前端工程师技能之photoshop巧用系列第二篇——测量篇

    × 目录 [1]测量信息 [2]实战 [3]注意事项 前面的话 前端工程师使用photoshop进行的大量工作实际上是测量.本文是photoshop巧用系列第二篇——测量篇 测量信息 在网页制作中需要 ...

  6. 《javascript权威指南》读书笔记——第二篇

    <javascript权威指南>读书笔记——第二篇 金刚 javascript js javascript权威指南 今天是今年的196天,分享今天的读书笔记. 第2章 词法结构 2.1 字 ...

  7. [转]Android开源项目第二篇——工具库篇

    本文为那些不错的Android开源项目第二篇--开发工具库篇,主要介绍常用的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多 ...

  8. 第二篇.Bootstrap起步

    第二篇Bootstrap起步 我们可以在http://getbootstrap.com下载bootstrap的文件 点击左边的download bootstrap可以下载bootstrap的css,j ...

  9. 【OpenGL】第二篇 Hello OpenGL

    ---------------------------------------------------------------------------------------------------- ...

随机推荐

  1. 【转】asp.net c# 网上搜集面试题目大全(附答案)

    asp.net c# 网上搜集面试题目大全(附答案) http://www.cnblogs.com/hndy/articles/2234188.html

  2. 那么都数据库表,那么多不同记录。是怎样都存储在一个key-value数据库的?

    那么都数据库表,那么多不同记录.是怎样都存储在一个key-value数据库的? :设置不同的键值而已!不同的表,选出统一的key规范 jedis.sadd("tom:friend:list& ...

  3. eclipse SDK更新管理器安装插件

    1.在Eclipse的主菜单,点击help,选择Install New Software 2.Work with: Indigo - http://download.eclipse.org/relea ...

  4. /etc/hosts.conf

    一 作用       指定如何解析主机域名.可设置网络安全. 二 参数说明      默认情况,/etc/hosts.conf 文件有如下内容——      order hosts,bind     ...

  5. laravle faker

    1.编辑 /database/factories/ModelFactory,添加新的类模型填充 $factory->define(App\Post::class, function (Faker ...

  6. pdftoswf + flexpaper 图片转pdf浏览体验的实现

    需要的工具: pdftoswf:http://www.swftools.org/download.html flexpaper包,最好找个破解版的swf文件替换一下里面的swf文件.http://fi ...

  7. JavaScript函数参数与调用

    函数调用: /* 1. 函数调用 */ ,,,); /* 2. 方法调用 */ this.CName = "全局"; var o = { CName:"o类", ...

  8. (转)freemakeer初入门

    在web开发过程中,尤其是后台管理系统的开发中,少不了增删改成的基础操作,原来我自己的做法是一份一份的拷贝粘贴,然后修改其中的不同,然而这样既枯燥无味又浪费了大量的时间,所以根据自己项目结构的特点写了 ...

  9. MySQL- 锁(2)

    InnoDB行锁实现方式 InnoDB行锁是通过给索引上的索引项加锁来实现的,这一点MySQL与Oracle不同,后者是通过在数据块中对相应数据行加锁来实现的.InnoDB这种行锁实现特点意味着:只有 ...

  10. 基于vs2005以上版本Qt程序发布的注意事项(讲了manifest的问题)

    最近发现了一个非常恼人的程序deployment的问题,估计大家有可能也会遇到,特此memo. 问题的出现我觉得主要还是微软搞的花头太多, 一个不知所谓的manifest文件让本来简单的程序发布变得困 ...