iOS有关通讯录操作
一、首先获取用户通讯录授权信息。
- (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); }
当然还有很多属性,具体可以进入文件查看其属性。
四、如果你对通讯录的添加删除感兴趣,可以参考
- (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开发——高级篇——通讯录
iOS有关通讯录操作的更多相关文章
- ios读取通讯录信息
ios读取通讯录信息 (2012-05-22 14:07:11) 标签: ios读取通讯录 it iphone如许app读取通讯录信息,读取通讯录信息时需要加载AddressBookUI 和Add ...
- iOS 修改通讯录联系人地址(address)崩溃原因分析
目前项目中需要对iOS系统通讯录进行读取,修改操作.在进行对地址修改的时候,出现了一个奇怪现象: ● 如果contact没有address字段(或者一个全新的contact),对它的address进行 ...
- ios 获取通讯录的所有信息
iOS获取通讯录全部信息 ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBoo ...
- iOS多线程拾贝------操作巨人编程
iOS多线程拾贝------操作巨人编程 多线程 基本 实现方案:pthread - NSThread - GCD - NSOperation Pthread 多平台,可移植 c语言,要程序员管理生命 ...
- iOS - SQLite Database 操作数据库
iOS - SQLite Database 操作数据库 Sqlite 能被用在ios上做数据处理用,只要你懂得一点sql 就很容易使用sqlite 1:创建一个简单的View based appl ...
- IOS各种手势操作实例
先看下效果 手势相关的介绍 IOS中手势操作一般是 UIGestureRecognizer 类的几个手势子类去实现,一般我们用到的手势就这么5种: 1.点击 UITapGestureRecogniz ...
- iOS子线程操作检测版本更新,有新版本通知用户更新, CheckVersion
iOS子线程操作检测版本更新,有新版本通知用户更新 CheckVersion 一:如何使用: #import "CheckVersion.h" //输入你的app在appStore ...
- iOS 本地通知 操作
iOS 本地通知 操作 1:配置通知:然后退出程序: UILocalNotification *localNotif = [[UILocalNotification alloc] init]; loc ...
- iOS 通讯录操作
转载至:http://superuna.blog.51cto.com/4192682/982938 //新增联系人 -(void)AddPeople { //取得本地通信录名柄 ...
随机推荐
- 【数据结构】单链表介绍及leetcode206题反转单链表python实现
题目传送门:https://leetcode-cn.com/problems/reverse-linked-list/ 文章目录 单链表介绍 链表 概念 种类 优缺点 单链表(slist) leetc ...
- Python笔记_第四篇_高阶编程_高阶函数_3.sorted
1. sorted函数: 常用的排序分:冒泡排序.选择排序.快速排序.插入排序.计数器排序 实例1:普通排序 # 普通排序 list1 = [,,,,] list2 = sorted(list1) # ...
- SSh三大框架的作用
一.详细分析spring+hibernate+struts作用? 1.struts是框架的表现层,Struts是对MVC构架的具体实现 Struts的MVC三层结构: (1)视图层:Struts采用J ...
- image compression with libjpeg
http://www.aaronmr.com/en/2010/03/test/ Working on the project I've seen in the need for compression ...
- SVN提交时忽略不必提交的文件夹和文件,如node_modules
空白处右键>选中TortoiseSVN>设置(settings)>常规设置(General)>Subversion>编辑(edit)>在弹出的config文件中找g ...
- 用IDLE调试python程序
1. 设置断点 先放例子: import pdb a=1 b=10 pdb.set_trace()#这里是断点 c=a+b print(c) import pdb 后,用pdb.set_trace() ...
- jQuery ajax中的dataType——JSON和JSONP
引用:http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html [原创]说说JSON和JSONP,也许你会豁 ...
- Office文档WEB端在线浏览(转换成Html)
最近在做了一个项目,要求是对Office文档在线预览.下面给大家分享一下我的方法. 1.第一种方法(不建议使用)我是在网上搜了一个利用COM组件对office文档进行转换,但是此方法必须要装Offic ...
- 关于vue内只要html元素的代码
使用v-model v-text v-html vue会解析出入的html代码报错 则上传sku的description时需要html页面的代码 所以在description 所在的表单元素内加入 v ...
- goweb-处理静态资源
处理静态文件 对于 HTML 页面中的 css 以及 js 等静态文件,需要使用使用 net/http 包下的以下 方法来处理 1) StripPrefix 函数 2) FileServer 函数 3 ...