IOS 最新通讯录和<Contacts/Contacts.h>框架
2015-12-29 14:24
483 查看
iOS 9 中,苹果介绍了新的 Contacts framework。允许用户使用 Objective-C 的 API 和设备的通讯录进行交互,同样适用于 Swift 语言。比起之前通过 AddressBook framework
来读取联系人信息来说,这是一个巨大的进步。因为 AddressBook framework 没有 Objective-C 的 API,非常难用,用 Swift 写的时候更是痛苦。现在就有一份Object-OC的版本。
第一步效果图:
第二部:我们看一些关键的代码。
1、创建获取数据的对象。
CNContactStore * ContactStore;
2、创建一个代理,已提供全工程。
// 初始化对象
-(PhoneBookManager*)PhoneBookShare{
static PhoneBookManager * Manger =
nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Manger = [[self
class] alloc];
});
return Manger;
}
3、判断是否,允许获取通讯录数据
-(BOOL)ExamineGetPhoneBookOfJurisdiction{
// 获取枚举类型,是否允许获取权限的状态
CNAuthorizationStatus AuthorizationStatus = [CNContactStore
authorizationStatusForEntityType:CNEntityTypeContacts];
if (AuthorizationStatus ==
CNAuthorizationStatusNotDetermined) {
return NO;
}else{
return YES;
}
}
4、规定从通讯录获取的数据类型。
NSArray * TypeArray =
@[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactPhoneNumbersKey,CNContactEmailAddressesKey,CNContactBirthdayKey,CNContactDepartmentNameKey];
5、进行数据的请求
CNContactFetchRequest * Request = [[
CNContactFetchRequest alloc]initWithKeysToFetch:TypeArray];
6、进行数据的处理。
[ContactStore
enumerateContactsWithFetchRequest:Request
error:nil
usingBlock:^(CNContact *
_Nonnull contact,
BOOL *
_Nonnull stop) {
/*
* CNLabeledValue
该类在json 转化二进制流的时候,不能被转化。切记一定要转化,否者也存不到文件里面
*/
7、Tableview 对象的初始化。
-(void)createTableview{
//
初始化
PhoneBook = [[UITableView
alloc]initWithFrame:CGRectMake(0,
66,
self.view.frame.size.width,
self.view.frame.size.height
- 66)
style:UITableViewStylePlain];
//
设置代理
PhoneBook.dataSource =
self;
PhoneBook.delegate =
self;
// 去掉多余的分割线
UIView * FootView = [[UIView
alloc]init];
PhoneBook.tableFooterView = FootView;
[self.view
addSubview:PhoneBook];
}
8、Cell 的创建。
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString * CellID = [NSString
stringWithFormat:@"IDF%d-%d",(int)indexPath.section,(int)indexPath.row];
UITableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier:CellID];
if (!cell) {
// cell
的创建与初始化
cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellID];
// 添加手势
UILongPressGestureRecognizer * LongP = [[UILongPressGestureRecognizer
alloc]initWithTarget:self
action:@selector(LongPClick:)];
[cell addGestureRecognizer:LongP];
//
巧用UILabel传递数据
UILabel * M = [[UILabel
alloc]init];
M.text = [NSString
stringWithFormat:@"%d*%d",(int)indexPath.section,(int)indexPath.row];
M.tag = 1000;
[cell addSubview:M];
UIImageView * ImageV = [[UIImageView
alloc]initWithFrame:CGRectMake(10,
10, 60,
60)];
//
获取随机数据
int count =
arc4random()%6;
//添加数据
ImageV.image = [UIImage
imageNamed:[NSString
stringWithFormat:@"%d.jpg",count]];
// 对图片处理
ImageV.contentMode =
UIViewContentModeScaleAspectFill;
//
进行圆角裁剪
ImageV.layer.masksToBounds =
YES;
ImageV.layer.cornerRadius =
15;
[cell addSubview:ImageV];
// 姓名
UILabel * NameLable = [[UILabel
alloc]initWithFrame:CGRectMake(CGRectGetWidth(ImageV.frame)+20,
10, 200,
30)];
NameLable.tag =
100;
[cell addSubview:NameLable];
// 电话话好吗
UILabel * PhoneLable = [[UILabel
alloc]initWithFrame:CGRectMake(CGRectGetWidth(ImageV.frame)+20,
40, 200,
30)];
PhoneLable.tag =
200;
[cell addSubview:PhoneLable];
//
打电话按钮的添加
UIButton * TelBtn = [UIButton
buttonWithType:UIButtonTypeCustom];
TelBtn.frame =
CGRectMake(self.view.frame.size.width-70,
20, 70,
70);
TelBtn.titleLabel.font = [UIFont
systemFontOfSize:18];
[TelBtn setImage:[UIImage
imageNamed:@"phone.png"]
forState:UIControlStateNormal];
TelBtn.imageView.contentMode =
UIViewContentModeScaleAspectFit;
[TelBtn addTarget:self
action:@selector(TelClick:)
forControlEvents:UIControlEventTouchUpInside];
TelBtn.titleLabel.text = [NSString
stringWithFormat:@"%ld-%ld",indexPath.section,indexPath.row];
[cell addSubview:TelBtn];
}
// 给名字,添加数据
UILabel * CellNameLabel = (UILabel*)[cell
viewWithTag:100];
CellNameLabel.text =
_PhoneBooksArray[indexPath.section][[NSString
stringWithFormat:@"Array"]][indexPath.row][@"name"];
// 给电话号码,添加数据
UILabel * CellPhoneLabel = (UILabel*)[cell
viewWithTag:200];
CellPhoneLabel.text =
_PhoneBooksArray[indexPath.section][[NSString
stringWithFormat:@"Array"]][indexPath.row][@"phones"][0];
return cell;
}
9、 几个主要代理的实现。
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString * DS = [NSString
stringWithFormat:@"Array"];
// 进行多余的删除(空的数组)
if ([_PhoneBooksArray[section][DS]
count] == 0) {
return @"";
}else{
return [NSString
stringWithFormat:@"%@",_PhoneBooksArray[section][@"ZSJ"]];
}
}
// 返回,检索的字母
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSMutableArray * Array = [NSMutableArray
arrayWithCapacity:0];
for (int i =0 ;i<26 ; i++) {
//
以字符的方式输出
[Array addObject:[NSString
stringWithFormat:@"%c",65+i]];
}
//
进行数据的插入
[Array insertObject:@"❤"
atIndex:0];
return Array;
}
// 进行检索
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
return index;
}
// 让cell
选中不变色
-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
return
NO;
}
10、打电话的接口。
//
一行代码调通电话接口
[ZSJTELCALL CallPhoneNumber:TelNumber
Call:^(NSTimeInterval Duration) {
//
通话时间提醒
} Cancel:^{
// 通话结束
}];
来读取联系人信息来说,这是一个巨大的进步。因为 AddressBook framework 没有 Objective-C 的 API,非常难用,用 Swift 写的时候更是痛苦。现在就有一份Object-OC的版本。
第一步效果图:
第二部:我们看一些关键的代码。
1、创建获取数据的对象。
CNContactStore * ContactStore;
2、创建一个代理,已提供全工程。
// 初始化对象
-(PhoneBookManager*)PhoneBookShare{
static PhoneBookManager * Manger =
nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Manger = [[self
class] alloc];
});
return Manger;
}
3、判断是否,允许获取通讯录数据
-(BOOL)ExamineGetPhoneBookOfJurisdiction{
// 获取枚举类型,是否允许获取权限的状态
CNAuthorizationStatus AuthorizationStatus = [CNContactStore
authorizationStatusForEntityType:CNEntityTypeContacts];
if (AuthorizationStatus ==
CNAuthorizationStatusNotDetermined) {
return NO;
}else{
return YES;
}
}
4、规定从通讯录获取的数据类型。
NSArray * TypeArray =
@[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactPhoneNumbersKey,CNContactEmailAddressesKey,CNContactBirthdayKey,CNContactDepartmentNameKey];
5、进行数据的请求
CNContactFetchRequest * Request = [[
CNContactFetchRequest alloc]initWithKeysToFetch:TypeArray];
6、进行数据的处理。
[ContactStore
enumerateContactsWithFetchRequest:Request
error:nil
usingBlock:^(CNContact *
_Nonnull contact,
BOOL *
_Nonnull stop) {
/*
* CNLabeledValue
该类在json 转化二进制流的时候,不能被转化。切记一定要转化,否者也存不到文件里面
*/
7、Tableview 对象的初始化。
-(void)createTableview{
//
初始化
PhoneBook = [[UITableView
alloc]initWithFrame:CGRectMake(0,
66,
self.view.frame.size.width,
self.view.frame.size.height
- 66)
style:UITableViewStylePlain];
//
设置代理
PhoneBook.dataSource =
self;
PhoneBook.delegate =
self;
// 去掉多余的分割线
UIView * FootView = [[UIView
alloc]init];
PhoneBook.tableFooterView = FootView;
[self.view
addSubview:PhoneBook];
}
8、Cell 的创建。
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString * CellID = [NSString
stringWithFormat:@"IDF%d-%d",(int)indexPath.section,(int)indexPath.row];
UITableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier:CellID];
if (!cell) {
// cell
的创建与初始化
cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellID];
// 添加手势
UILongPressGestureRecognizer * LongP = [[UILongPressGestureRecognizer
alloc]initWithTarget:self
action:@selector(LongPClick:)];
[cell addGestureRecognizer:LongP];
//
巧用UILabel传递数据
UILabel * M = [[UILabel
alloc]init];
M.text = [NSString
stringWithFormat:@"%d*%d",(int)indexPath.section,(int)indexPath.row];
M.tag = 1000;
[cell addSubview:M];
UIImageView * ImageV = [[UIImageView
alloc]initWithFrame:CGRectMake(10,
10, 60,
60)];
//
获取随机数据
int count =
arc4random()%6;
//添加数据
ImageV.image = [UIImage
imageNamed:[NSString
stringWithFormat:@"%d.jpg",count]];
// 对图片处理
ImageV.contentMode =
UIViewContentModeScaleAspectFill;
//
进行圆角裁剪
ImageV.layer.masksToBounds =
YES;
ImageV.layer.cornerRadius =
15;
[cell addSubview:ImageV];
// 姓名
UILabel * NameLable = [[UILabel
alloc]initWithFrame:CGRectMake(CGRectGetWidth(ImageV.frame)+20,
10, 200,
30)];
NameLable.tag =
100;
[cell addSubview:NameLable];
// 电话话好吗
UILabel * PhoneLable = [[UILabel
alloc]initWithFrame:CGRectMake(CGRectGetWidth(ImageV.frame)+20,
40, 200,
30)];
PhoneLable.tag =
200;
[cell addSubview:PhoneLable];
//
打电话按钮的添加
UIButton * TelBtn = [UIButton
buttonWithType:UIButtonTypeCustom];
TelBtn.frame =
CGRectMake(self.view.frame.size.width-70,
20, 70,
70);
TelBtn.titleLabel.font = [UIFont
systemFontOfSize:18];
[TelBtn setImage:[UIImage
imageNamed:@"phone.png"]
forState:UIControlStateNormal];
TelBtn.imageView.contentMode =
UIViewContentModeScaleAspectFit;
[TelBtn addTarget:self
action:@selector(TelClick:)
forControlEvents:UIControlEventTouchUpInside];
TelBtn.titleLabel.text = [NSString
stringWithFormat:@"%ld-%ld",indexPath.section,indexPath.row];
[cell addSubview:TelBtn];
}
// 给名字,添加数据
UILabel * CellNameLabel = (UILabel*)[cell
viewWithTag:100];
CellNameLabel.text =
_PhoneBooksArray[indexPath.section][[NSString
stringWithFormat:@"Array"]][indexPath.row][@"name"];
// 给电话号码,添加数据
UILabel * CellPhoneLabel = (UILabel*)[cell
viewWithTag:200];
CellPhoneLabel.text =
_PhoneBooksArray[indexPath.section][[NSString
stringWithFormat:@"Array"]][indexPath.row][@"phones"][0];
return cell;
}
9、 几个主要代理的实现。
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString * DS = [NSString
stringWithFormat:@"Array"];
// 进行多余的删除(空的数组)
if ([_PhoneBooksArray[section][DS]
count] == 0) {
return @"";
}else{
return [NSString
stringWithFormat:@"%@",_PhoneBooksArray[section][@"ZSJ"]];
}
}
// 返回,检索的字母
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSMutableArray * Array = [NSMutableArray
arrayWithCapacity:0];
for (int i =0 ;i<26 ; i++) {
//
以字符的方式输出
[Array addObject:[NSString
stringWithFormat:@"%c",65+i]];
}
//
进行数据的插入
[Array insertObject:@"❤"
atIndex:0];
return Array;
}
// 进行检索
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
return index;
}
// 让cell
选中不变色
-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
return
NO;
}
10、打电话的接口。
//
一行代码调通电话接口
[ZSJTELCALL CallPhoneNumber:TelNumber
Call:^(NSTimeInterval Duration) {
//
通话时间提醒
} Cancel:^{
// 通话结束
}];
相关文章推荐
- Android编程基于Contacts读取联系人的方法(附demo源码)
- ContactsContract.Contacts与ContactsContract.CommonDataKinds.Phone的区别
- AddressBookUI Framework in iOS9 beta4
- iOS---iOS9中获取通讯录的方法:Contacts. framework简单使用
- 获取相机、麦克风、通讯录权限,确认是否拥有这些权限
- Contacts启动
- Android下操作联系人(Contacts)
- Android ContentProvider 分析与应用(一)使用ContentProvider
- android 监控联系人变化
- Android中查找联系人常用的URI
- 玩转android本地Content Provider【Android】
- Android Contacts数据库分析
- 在android中读取联系人信息的程序,包括读取联系人姓名、手机号码和邮箱
- Android联系人Contacts详解
- 非常高效地读手机联系人列表和分组
- AndroidContacts操作系列之 查看底层联系人表结构
- Android Contacts 联系人源码分析
- Android Contacts的使用(一)
- Android Contacts的使用(二)
- Android Contacts的使用(三)