统计
  • 建站日期:2021-03-10
  • 文章总数:386 篇
  • 评论总数:415 条
  • 分类总数:9 个
  • 最后更新:10月20日
文章 未分类

iOS技巧之获取本机通讯录中的内容,解析通讯录源代码

创新博客
首页 未分类 正文

一、在工程中添加AddressBook.framework和AddressBookUI.framework

二、获取通讯录

1、在infterface中定义数组并在init方法中初始化

NSMutableArray *addressBookTemp;- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{addressBookTemp = [NSMutableArray array];
}

2、定义一个model,用来存放通讯录中的各个属性
新建一个继承自NSObject的类,在.h中

@interface TKAddressBook : NSObject {NSInteger sectionNumber;NSInteger recordID;NSString *name;NSString *email;NSString *tel;
}
@property NSInteger sectionNumber;
@property NSInteger recordID;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *tel;@end

在.m文件中进行synthesize

@implementation TKAddressBook
@synthesize name, email, tel, recordID, sectionNumber;@end

3、获取联系人

在iOS6之后,获取通讯录需要获得权限

//新建一个通讯录类ABAddressBookRef addressBooks = nil;if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0){addressBooks =  ABAddressBookCreateWithOptions(NULL, NULL);//获取通讯录权限dispatch_semaphore_t sema = dispatch_semaphore_create(0);ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);});dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);dispatch_release(sema);}else{addressBooks = ABAddressBookCreate();}//获取通讯录中的所有人
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);
//通讯录中人数
CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);//循环,获取每个人的个人信息
for (NSInteger i = 0; i < nPeople; i++){//新建一个addressBook model类TKAddressBook *addressBook = [[TKAddressBook alloc] init];//获取个人ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);//获取个人名字CFTypeRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty);CFTypeRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);CFStringRef abFullName = ABRecordCopyCompositeName(person);NSString *nameString = (__bridge NSString *)abName;NSString *lastNameString = (__bridge NSString *)abLastName;if ((__bridge id)abFullName != nil) {nameString = (__bridge NSString *)abFullName;} else {if ((__bridge id)abLastName != nil){nameString = [NSString stringWithFormat:@"%@ %@", nameString, lastNameString];}}addressBook.name = nameString;addressBook.recordID = (int)ABRecordGetRecordID(person);;ABPropertyID multiProperties[] = {kABPersonPhoneProperty,kABPersonEmailProperty};NSInteger multiPropertiesTotal = sizeof(multiProperties) / sizeof(ABPropertyID);for (NSInteger j = 0; j < multiPropertiesTotal; j++) {ABPropertyID property = multiProperties[j];ABMultiValueRef valuesRef = ABRecordCopyValue(person, property);NSInteger valuesCount = 0;if (valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef);if (valuesCount == 0) {CFRelease(valuesRef);continue;}//获取电话号码和emailfor (NSInteger k = 0; k < valuesCount; k++) {CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k);switch (j) {case 0: {// Phone numberaddressBook.tel = (__bridge NSString*)value;break;}case 1: {// EmailaddressBook.email = (__bridge NSString*)value;break;}}CFRelease(value);}CFRelease(valuesRef);}//将个人信息添加到数组中,循环完成后addressBookTemp中包含所有联系人的信息[addressBookTemp addObject:addressBook];if (abName) CFRelease(abName);if (abLastName) CFRelease(abLastName);if (abFullName) CFRelease(abFullName);}

三、显示在table中
//行数

//行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;
}//列数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return [addressBookTemp count];
}
//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {NSString *cellIdentifier = @"ContactCell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];if (cell == nil){cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];}TKAddressBook *book = [addressBookTemp objectAtIndex:indexPath.row];cell.textLabel.text = book.name;cell.detailTextLabel.text = book.tel;return cell;
}

版权说明
文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。
版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!

这篇文章最后更新于2023-5-5,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!
去水印图片软件系统水印管家一键去水印windows绿色版免安装软件短视频图片去水印
« 上一篇
layui+php实现后台页面展示搜索分页排序删除
下一篇 »
为了防止灌水评论,登录后即可评论!

HI ! 请登录
注册会员,享受下载全站资源特权。

最新文章

热门文章