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
Tags: additional interface property loading 通訊錄
文章來源: