一、首先获取用户通讯录授权信息。

在AppDelegate中导入#import <AddressBook/AddressBook.h>框架,在下列方法中获取授权信息。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

具体代码:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

     // 1.获取通讯录授权状态
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
// 2.授权申请
if (status == kABAuthorizationStatusNotDetermined) {
// 有create就一定有release
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); ABAddressBookRequestAccessWithCompletion(book, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"授权允许");
}else {
NSLog(@"授权拒绝");
}
}); CFRelease(book);
}
return YES;
}

二、对通讯录联系人属性进行的一系列操作

 - (void)viewDidLoad {
[super viewDidLoad];
// 1.创建通讯录
ABAddressBookRef book = ABAddressBookCreate();
// 2.得到所有通讯录
CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(book); for (NSUInteger i=; i<CFArrayGetCount(results); i++) {
ABRecordRef person = CFArrayGetValueAtIndex(results, i); // 读取firstName
NSString *personName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
if (personName != nil) {
NSLog(@"名:%@", personName);
} // 获取lastName
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
if (lastName != nil) {
NSLog(@"姓:%@", lastName);
} // NSString *lastNamePhonetic = (__bridge NSString *)(ABRecordCopyValue(book, kABPersonLastNamePhoneticProperty));
// if (lastNamePhonetic != nil) {
// NSLog(@"%@", lastNamePhonetic);
// }
//读取organization公司
NSString *organization = (__bridge NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);
if(organization != nil) NSLog(@"%@", organization); //获取email多值
ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);
int emailcount = ABMultiValueGetCount(email);
for (int x = ; x < emailcount; x++)
{
//获取email Label
NSString* emailLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(email, x));
//获取email值
NSString* emailContent = (__bridge NSString*)ABMultiValueCopyValueAtIndex(email, x);
NSLog(@"emailLabel:%@,emailContent:%@",emailLabel,emailContent);
}
//读取地址多值
ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
int count = ABMultiValueGetCount(address); for(int j = ; j < count; j++)
{
//获取地址Label
NSString* addressLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(address, j);
NSLog(@"%@",addressLabel);
//获取該label下的地址6属性
NSDictionary* personaddress =(__bridge NSDictionary*) ABMultiValueCopyValueAtIndex(address, j);
NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];
if(country != nil)
NSLog(@"国家:%@\n",country);
NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];
if(city != nil)
NSLog(@"城市:%@\n",city);
NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];
if(state != nil)
NSLog(@"省:%@\n",state);
NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];
if(street != nil)
NSLog(@"街道:%@\n",street);
NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey];
if(zip != nil)
NSLog(@"邮编:%@\n",zip);
NSString* coutntrycode = [personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey];
if(coutntrycode != nil)
NSLog(@"国家编号:%@\n",coutntrycode);
}
//第一次添加该条记录的时间
NSString *firstknow = (__bridge NSString*)ABRecordCopyValue(person, kABPersonCreationDateProperty);
NSLog(@"第一次添加该条记录的时间%@\n",firstknow);
//最后一次修改該条记录的时间
NSString *lastknow = (__bridge NSString*)ABRecordCopyValue(person, kABPersonModificationDateProperty);
NSLog(@"最后一次修改该条记录的时间%@\n",lastknow); //读取电话多值
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (int k = ; k<ABMultiValueGetCount(phone); k++)
{
//获取电话Label
NSString * personPhoneLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));
//获取該Label下的电话值
NSString * personPhone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phone, k); NSLog(@"%@:%@\n",personPhoneLabel,personPhone);
} //获取URL多值
ABMultiValueRef url = ABRecordCopyValue(person, kABPersonURLProperty);
for (int m = ; m < ABMultiValueGetCount(url); m++)
{
//获取电话Label
NSString * urlLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(url, m));
//获取該Label下的电话值
NSString * urlContent = (__bridge NSString*)ABMultiValueCopyValueAtIndex(url,m); NSLog(@"%@:%@\n",urlLabel,urlContent);
} //读取照片
NSData *image = (__bridge NSData*)ABPersonCopyImageData(person); UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
[myImage setImage:[UIImage imageWithData:image]];
myImage.opaque = YES;
[self.view addSubview:myImage]; } CFRelease(book);
CFRelease(results);
}

这里只是获取了通讯录联系人的一部分属性。获取更多的属性参考《iOS 获取通讯录中联系人的所有属性

三、获取这些属性你也可以利用三方框架RHAddressBook 参考: ios中访问通讯录数据

引入头文件,这里我用的是cocopods管理这个三方框架,引入框架同时 #import <RHAddressBook.h>,还要引入 #import <RHAddressBook/RHPerson.h>这个头文件。具体代码:

  // 创建通讯录对象
RHAddressBook *book = [[RHAddressBook alloc] init];
// 获取通讯录所有联系人
NSArray *peopleArray = book.people; for (RHPerson *people in peopleArray) { //获取人员的firstName
NSString* firstName = people.firstName;
//获取人员的lastName
NSString* lastName = people.lastName;
//获取该人员的号码(号码有多个,所以用RHMultiValue)
RHMultiValue* phoneNumbers = people.phoneNumbers;
NSUInteger phoneNumberCount = phoneNumbers.count;
for (int i = ; i < phoneNumberCount; i++) {
//遍历每个号码中的label(比如:手机 家庭 公司)
NSString* label = [phoneNumbers labelAtIndex:i];
//遍历出号码
NSString* nember = [phoneNumbers valueAtIndex:i];
NSLog(@"%@, %@ ,%@,%@",firstName,lastName,label,nember);
}
NSLog(@"%@", people); }

当然还有很多属性,具体可以进入文件查看其属性。

四、如果你对通讯录的添加删除感兴趣,可以参考

《iOS添加、删除通讯录》

 - (void)addAddressBook
{
// 创建一个通讯录操作对象
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
// 创建新的联系人记录
ABRecordRef newRecord = ABPersonCreate();
NSString *firstName = @"五";
NSString *lastName = @"王";
// 为新的联系人添加属性值
ABRecordSetValue(newRecord, kABPersonFirstNameProperty, (__bridge CFTypeRef)(firstName), NULL);
ABRecordSetValue(newRecord, kABPersonLastNameProperty, (__bridge CFTypeRef)(lastName), NULL); // 创建一个多值属性
ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType); NSString *mobeileLabel = @"";
ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)(mobeileLabel), kABPersonPhoneMobileLabel, NULL); // 将多值属性添加到记录
ABRecordSetValue(newRecord, kABPersonPhoneProperty, multi, NULL); // 添加记录到通讯录操作对象
ABAddressBookAddRecord(addressBook, newRecord, NULL); CFRelease(multi);
CFRelease(newRecord);
}
});
ABAddressBookSave(addressBook, NULL);
CFRelease(addressBook);
}

五、推荐一篇值得学习的文章  iOS开发——高级篇——通讯录

 

最新文章

  1. No.014 Longest Common Prefix
  2. LeetCode——Find Median from Data Stream
  3. C#中往数据库插入/更新时候关于NUll空值的处理
  4. oracle安装,配置,启动
  5. Rechability的简单使用
  6. No orientation specified, and the default is
  7. C# Best Practices - Handling Strings
  8. Android学习笔记:ListView简单应用--显示文字列表
  9. Cygwin下vim按方向键出现ABCD;
  10. 算法入门(C++)
  11. Redis进阶实践之十三 Redis的Redis-trib.rb文件详解
  12. jsp学习笔记之:4种基本语法
  13. java字符串实现正序和倒序输出
  14. UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode characters in position 1-5: ordinal not in range(128)
  15. JAVA课后作业01
  16. 763. Partition Labels 相同字母出现在同一块中,且块数最多
  17. RestTemplate -springwebclient
  18. Revit API创建房间
  19. JQuery设置和去除disabled属性 与 display显示隐藏
  20. Smashing The Browser:From Vulnerability Discovery To Exploit学习记录

热门文章

  1. Python说文解字_杂谈09
  2. 基于python的爬虫流程图(精简版)
  3. 微信公众号关联小程序AppID是什么
  4. DVWA-目录遍历-文件包含
  5. 怎样在 Akka Persistence 中实现分页查询
  6. 素小暖讲JVM:Eclipse运行速度调优
  7. python paramiko登陆设备
  8. UI Automation技术获取cmd或Powershell命令提示符窗口的实时内容
  9. c指针(2)
  10. 使用meshgrid生成热图和单位向量场