十二,iOS通讯录好友信息的获取
来源:互联网 发布:淘宝企业店铺出售 编辑:程序博客网 时间:2024/05/18 01:37
1,先是通过方法获取数据,然后通过tableview展示出来,在底部有访问按钮
2,首先要导入头文件#import <ContactsUI/ContactsUI.h>,其次声明代理CNContactPickerDelegate
3,具体代码如下,其中有些是自己定义的宏看着改下
#define W [UIScreen mainScreen].bounds.size.width
#define H [UIScreen mainScreen].bounds.size.height
#import "ContactAccessViewController.h"
#import <ContactsUI/ContactsUI.h>
@interface ContactAccessViewController ()<CNContactPickerDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) NSArray *menuList;
@property (nonatomic, strong) NSArray *infoList;
@property (nonatomic, strong) UIButton *contactBtn;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end
@implementation ContactAccessViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self initUI];
[self setFrame];
_menuList = [NSArray arrayWithObjects:
@"姓名",
@"公司名称",
@"电话号码",
@"邮箱地址",
nil];
_dataSource = [NSMutableArray arrayWithObjects:@[@""],@[@""],@[@""],@[@""],nil];
}
- (void)initUI{
_table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_table.backgroundColor = [UIColor clearColor];
_table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
_table.delegate = self;
_table.dataSource = self;
[self.view addSubview:_table];
_contactBtn = [[UIButton alloc]init];
// _contactBtn.backgroundColor = DB_Red;
_contactBtn.backgroundColor = [UIColor redColor];
[_contactBtn setTitle:@"访问通讯录" forState:UIControlStateNormal];
_contactBtn.layer.cornerRadius = 5.0;
[_contactBtn addTarget:self action:@selector(contactAcesss) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_contactBtn];
}
- (void)setFrame{
CGFloat tableY = 64 + 10;
CGFloat tableH = H - tableY - 100;
_table.frame = CGRectMake(0, tableY, W, tableH);
CGFloat tfX = 20;
CGFloat tfY = CGRectGetMaxY(_table.frame) + 20;
CGFloat tfwidth = self.view.frame.size.width - 40;
CGFloat tfheight = 44;
_contactBtn.frame = CGRectMake(tfX, tfY, tfwidth, tfheight);
}
- (void)contactAcesss{
CNContactPickerViewController * contactVc = [CNContactPickerViewController new];
contactVc.delegate = self;
[self presentViewController:contactVc animated:YES completion:^{
}];
}
#pragma mark - 用户点击联系人获取方法 两个方法都写只调用此方法
-(void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{
[_dataSource removeAllObjects];
// NSLog(@"contact : %@",contact);
// 姓氏 名字
NSString *namestr = [NSString stringWithFormat:@"%@%@",contact.familyName,contact.givenName];
NSMutableArray * arrname = [NSMutableArray array];
[arrname addObject:namestr];
[_dataSource addObject:arrname];
//公司名
NSLog(@"_dataSource%@",_dataSource);
NSLog(@"公司: %@",contact.organizationName);
// [_dataSource addObject:[NSString stringWithFormat:@"%@",contact.organizationName]];
NSMutableArray * managename = [NSMutableArray array];
[managename addObject:[NSString stringWithFormat:@"%@",contact.organizationName]];
[_dataSource addObject:managename];
NSLog(@"_dataSource 2 %@",_dataSource);
//获取通讯录某个人所有电话并存入数组中 需要哪个取哪个
NSMutableArray * arrMPhoneNums = [NSMutableArray array];
for (CNLabeledValue * labValue in contact.phoneNumbers) {
NSString * strPhoneNums = [labValue.value stringValue];
NSLog(@"所有电话是: %@",strPhoneNums);
[arrMPhoneNums addObject:strPhoneNums];
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strPhoneNums]];
}
[_dataSource addObject:arrMPhoneNums];
NSLog(@"_dataSource 3 %@",_dataSource);
//所有邮件地址数组
NSMutableArray * arrMEmails = [NSMutableArray array];
for (CNLabeledValue * labValue in contact.emailAddresses) {
NSLog(@"email : %@",labValue.value);
[arrMEmails addObject:labValue.value];
}
[_dataSource addObject:arrMEmails];
NSLog(@"_dataSource 4 %@",_dataSource);
[_table reloadData];
[picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 用户点进去获取属性调用方法 例如从通讯录选择联系人打电话两个方法都写只调用上面方法
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty{
// NSLog(@"contactProperty : %@",contactProperty);
// NSLog(@"contact : %@",contactProperty.contact);
// NSLog(@"key : %@",contactProperty.key);
// [[UIApplication sharedApplication] openURL:url];
// NSLog(@"identifier : %@",contactProperty.identifier);
// NSLog(@"label : %@",contactProperty.label);
//获得点击的属性,在此进行处理...
NSLog(@"value : %@",[contactProperty.value stringValue]);
[picker dismissViewControllerAnimated:YES completion:nil];
}
//取消选择回调
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _menuList.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_dataSource[section] count];
}
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(20, 0, W - 40, 30)];
UILabel *label = [[UILabel alloc]init];
label.frame = CGRectMake(15, 0, W - 40, 30);
label.text = [_menuList objectAtIndex:section];
label.font = Font_CN(15);
[view addSubview:label];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
cell.textLabel.font = Font_CN(15);
cell.detailTextLabel.font = Font_CN(14);
// cell.textLabel.text = [_menuList objectAtIndex:indexPath.row];
// cell.detailTextLabel.text = [_infoList objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",_dataSource[indexPath.section][indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
- 十二,iOS通讯录好友信息的获取
- IOS 获取通讯录信息
- iOS获取通讯录信息
- 关于ios的通讯录信息获取
- IOS通讯录编程 获取通讯录信息
- webwxgetcontact 获取的是通讯录里的群或好友的信息
- iOS 获取通讯录联系人信息
- iOS 获取通讯录联系人信息
- ios 获取所有通讯录信息
- iOS获取通讯录联系人信息
- iOS获取通讯录联系人信息
- iOS 获取通讯录相关信息
- iOS获取通讯录联系人信息
- iOS获取通讯录联系人信息
- 获取通讯录好友邮箱!
- iOS-JNAddressBook 获取手机通讯录信息
- IOS获取系统通讯录联系人信息
- iOS - ABPeoplePickerNavigationController 获取手机通讯录信息
- Hibernate小知识点(many to one ,one to one的属性解释)
- c#拼接List
- 向SQL Server中的现有表中添加具有默认值的列
- 《机器学习实战》学习笔记之k-近邻算法3
- windows server 2012将计算机图标添加到桌面
- 十二,iOS通讯录好友信息的获取
- c3p0
- java再复习——代码执行的大体流程与内存分析
- python的zip()函数
- java jni 调用c++库
- 将图片转为base64编码格式
- 统计python脚本中 shell命令输出文本的行数
- 状态码说明
- h5和chrome禁止页面缓存的方法