iOS通讯录相关知识-浅析
- (void)viewDidLoad
{
[super viewDidLoad];
// 获得用户通讯录数据的一个引用
ABAddressBookRef addressBook = ABAddressBookCreate();
if (addressBook != nil){
NSLog(@"Successfully accessed the address book.");
// 查看通讯录状态:YES:通讯录改变,但还没有保存;NO;通讯录不需要保存
if (ABAddressBookHasUnsavedChanges(addressBook)){
BOOL doYouWantToSaveChanges = YES;
if (doYouWantToSaveChanges){
CFErrorRef saveError = NULL;
// 保存通讯录
if (ABAddressBookSave(addressBook, &saveError)){
// 保存成功
} else {
// 保存失败,查看CFErrorRef 来了解错误原因
}
} else {
// 放弃保存
ABAddressBookRevert(addressBook);
}
}else{
NSLog(@"No changes to the address book.");
}
CFRelease(addressBook);// 释放通讯录引用对象
} else {
NSLog(@"Could not access the address book.");
}
}
CFRelease(addressBook);
因为:ABAddressBookCreate 函数返回的数据库实例,在使用完后必须将它释放掉,可以使用 CFRelease Core Foundation 方法。
- (void)viewDidLoad
{
[super viewDidLoad];
ABAddressBookRef addressBook = ABAddressBookCreate();// 获得用户通讯录数据的一个引用
if (addressBook != nil){
NSLog(@"Successfully accessed the address book.");
// 返回通讯录中所有的数据
NSArray *arrayOfAllPeople = ( NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);
// 循环所有联系人数据
for (NSUInteger peopleCounter = 0; peopleCounter < [arrayOfAllPeople count]; peopleCounter++){
ABRecordRef thisPerson = (ABRecordRef)[arrayOfAllPeople objectAtIndex:peopleCounter];
NSLog(@"%@", thisPerson);
NSString *firstName = (NSString *) ABRecordCopyValue(thisPerson, kABPersonFirstNameProperty);
NSString *lastName = (NSString *) ABRecordCopyValue(thisPerson, kABPersonLastNameProperty);
NSLog(@"First Name = %@", firstName);
NSLog(@"Last Name = %@", lastName);
[self logPersonEmails:thisPerson];
}
CFRelease(addressBook);// 释放通讯录引用对象
}
}
// 邮箱的数据
- (void) logPersonEmails:(ABRecordRef)paramPerson{
if (paramPerson == NULL){
NSLog(@"The given person is NULL.");
return;
}
// 获得电子邮件数据,电子邮件是个多值属性,所以需要循环遍历
ABMultiValueRef emails =ABRecordCopyValue(paramPerson, kABPersonEmailProperty);
if (emails == NULL){
NSLog(@"This contact does not have any emails.");
return;
}
for (NSUInteger emailCounter = 0;emailCounter < ABMultiValueGetCount(emails);emailCounter++){
// 邮件类别
NSString *emailLabel = [(NSString *) ABMultiValueCopyLabelAtIndex(emails, emailCounter) autorelease];
NSString *localizedEmailLabel = [(NSString *) ABAddressBookCopyLocalizedLabel((CFStringRef)emailLabel) autorelease];
// 邮件值
NSString *emailValue=[(NSString *)ABMultiValueCopyValueAtIndex(emails, emailCounter) autorelease];
NSString *email = [NSString stringWithFormat:@"Label = %@, Localized Label = %@,Email = %@",emailLabel,localizedEmailLabel,emailValue];
NSLog(@"%@",email);
}
CFRelease(emails);
}
- (void)viewDidLoad
{
[super viewDidLoad];
ABAddressBookRef addressBook = ABAddressBookCreate();// 获得用户通讯录数据的一个引用
if (addressBook != NULL){
ABRecordRef anthonyRobbins = [self newPersonWithFirstName:@"TT"
lastName:@"DD"
inAddressBook:addressBook];
if (anthonyRobbins != NULL){
NSLog(@"添加成功!");
CFRelease(anthonyRobbins);
}
CFRelease(addressBook);
}
}
// 添加联系人到通讯录
- (ABRecordRef) newPersonWithFirstName:(NSString *)paramFirstName lastName:(NSString *)paramLastName inAddressBook:(ABAddressBookRef)paramAddressBook{
ABRecordRef result = NULL;
if (paramAddressBook == NULL){
NSLog(@"The address book is NULL.");
return NULL;
}
if ([paramFirstName length] == 0 && [paramLastName length] == 0){
NSLog(@"First name and last name are both empty.");
return NULL;
}
result = ABPersonCreate();// 创建一个新的联系人
if (result == NULL){
NSLog(@"Failed to create a new person.");
return NULL;
}
BOOL couldSetFirstName = NO;
BOOL couldSetLastName = NO;
CFErrorRef setFirstNameError = NULL;
CFErrorRef setLastNameError = NULL;
// 设置FirstName,并且返回是否设置成功
couldSetFirstName = ABRecordSetValue(result,kABPersonFirstNameProperty,(CFTypeRef)paramFirstName,&setFirstNameError);
// 设置LastName,并且返回是否设置成功
couldSetLastName = ABRecordSetValue(result, kABPersonLastNameProperty,(CFTypeRef)paramLastName,&setLastNameError);
// 设置其他属性,类似,但是还没有研究出来先mark一下吧
if (couldSetFirstName && couldSetLastName){
NSLog(@"Successfully set the first name and the last name of the person.");
} else {
NSLog(@"Failed to set the first name and/or last name of the person.");
}
CFErrorRef couldAddPersonError = NULL;
BOOL couldAddPerson = ABAddressBookAddRecord(paramAddressBook, result,&couldAddPersonError);
if (couldAddPerson){
NSLog(@"添加联系人-成功!");
} else {
NSLog(@"添加联系人-失败!");
CFRelease(result);
result = NULL;
return result;
}
// 更新通讯录
if (ABAddressBookHasUnsavedChanges(paramAddressBook)){
CFErrorRef couldSaveAddressBookError = NULL;
BOOL couldSaveAddressBook = ABAddressBookSave(paramAddressBook, &couldSaveAddressBookError);
if (couldSaveAddressBook){
NSLog(@"成功的保存到了通讯录中");
} else {
NSLog(@"保存到通讯录中失败");
}
}
return result;
}
ABRecordRef result = NULL;
result = ABPersonCreate();// 创建一个新的联系人
ABRecordSetValue(result, kABPersonLastNameProperty,(CFTypeRef)paramLastName,&setLastNameError);
添加的项,应该也要分单值和多值的情况。现在还没搞清楚多值类型(电话号码,电子邮件)如何添加。如果您知道或者了解,请不吝赐教!谢谢!
CFErrorRef couldAddPersonError = NULL;
BOOL couldAddPerson = ABAddressBookAddRecord(paramAddressBook, result,&couldAddPersonError);
// 更新通讯录
if (ABAddressBookHasUnsavedChanges(paramAddressBook)){
CFErrorRef couldSaveAddressBookError = NULL;
BOOL couldSaveAddressBook = ABAddressBookSave(paramAddressBook, &couldSaveAddressBookError);
}
- (void)viewDidLoad
{
[super viewDidLoad];
ABAddressBookRef addressBook = ABAddressBookCreate();// 获得用户通讯录数据的一个引用
if (addressBook != nil){
ABRecordRef personalCoachesGroup =[self newGroupWithName:@"测试分类"
inAddressBook:addressBook];
if (personalCoachesGroup != NULL){
NSLog(@"Successfully created the group.");
CFRelease(personalCoachesGroup);
} else {
NSLog(@"Could not create the group.");
}
CFRelease(addressBook);
}
}
// 添加群组到通讯录中
- (ABRecordRef) newGroupWithName:(NSString *)paramGroupName inAddressBook:(ABAddressBookRef)paramAddressBook{
ABRecordRef result = NULL;
if (paramAddressBook == NULL){
NSLog(@"The address book is nil.");
return NULL;
}
result = ABGroupCreate();// 创建一个分组
if (result == NULL){
NSLog(@"Failed to create a new group.");
return NULL;
}
BOOL couldSetGroupName = NO;
CFErrorRef error = NULL;
// 设置分组
couldSetGroupName = ABRecordSetValue(result, kABGroupNameProperty,(CFTypeRef)paramGroupName,&error);
if (couldSetGroupName){
BOOL couldAddRecord = NO;
CFErrorRef couldAddRecordError = NULL;
couldAddRecord = ABAddressBookAddRecord(paramAddressBook, result,&couldAddRecordError);
if (couldAddRecord){
NSLog(@"Successfully added the new group.");
if (ABAddressBookHasUnsavedChanges(paramAddressBook)){
BOOL couldSaveAddressBook = NO;
CFErrorRef couldSaveAddressBookError = NULL;
couldSaveAddressBook = ABAddressBookSave(paramAddressBook,&couldSaveAddressBookError);
if (couldSaveAddressBook){
NSLog(@"Successfully saved the address book.");
} else {
CFRelease(result);
result = NULL;
NSLog(@"Failed to save the address book.");
}
} else {
CFRelease(result);
result = NULL;
NSLog(@"No unsaved changes.");
}
} else {
CFRelease(result);
result = NULL;
NSLog(@"Could not add a new group.");
}
} else {
CFRelease(result);
result = NULL;
NSLog(@"Failed to set the name of the group."); }
return result;
}
ABRecordRef result = NULL;
result = ABGroupCreate();// 创建一个分组
// 设置分组
couldSetGroupName = ABRecordSetValue(result, kABGroupNameProperty,(CFTypeRef)paramGroupName,&error);
CFErrorRef couldAddRecordError = NULL;
couldAddRecord = ABAddressBookAddRecord(paramAddressBook, result,&couldAddRecordError);
// 更新通讯录
if (ABAddressBookHasUnsavedChanges(paramAddressBook)){
CFErrorRef couldSaveAddressBookError = NULL;
BOOL couldSaveAddressBook = ABAddressBookSave(paramAddressBook, &couldSaveAddressBookError);
}
ABAddressBookRef addressBook = ABAddressBookCreate();
if (addressBook != nil){
// 创建联系人
ABRecordRef richardBranson = [self newPersonWithFirstName:@"Richard"
lastName:@"Branson"
inAddressBook:addressBook];
if (richardBranson != NULL){
// 创建分组
ABRecordRef entrepreneursGroup = [self newGroupWithName:@"Entrepreneurs"
inAddressBook:addressBook];
if (entrepreneursGroup != NULL){
BOOL isSuccess=[self addPerson:richardBranson toGroup:entrepreneursGroup saveToAddressBook:addressBook];
if (isSuccess){
NSLog(@"Successfully");
} else {
NSLog(@"Failed ");
}
CFRelease(entrepreneursGroup);
} else {
NSLog(@"Failed to create the Entrepreneurs group.");
}
CFRelease(richardBranson);
} else {
NSLog(@"Failed to create an entity for Richard Branson."); }
CFRelease(addressBook);
} else {
NSLog(@"Address book is nil.");
}
// 把联系人添加到指定的组中
- (BOOL) addPerson:(ABRecordRef)paramPerson toGroup:(ABRecordRef)paramGroup saveToAddressBook:(ABAddressBookRef)paramAddressBook{
BOOL result = NO;
if (paramPerson == NULL ||paramGroup == NULL || paramAddressBook == NULL){
NSLog(@"Invalid parameters are given.");
return result;
}
CFErrorRef error = NULL;
result = ABGroupAddMember(paramGroup,paramPerson,&error);
if (result == NO){
NSLog(@"Could not add the person to the group.");
return result;
}
if (ABAddressBookHasUnsavedChanges(paramAddressBook)){
BOOL couldSaveAddressBook = NO;
CFErrorRef couldSaveAddressBookError = NULL;
couldSaveAddressBook = ABAddressBookSave(paramAddressBook, &couldSaveAddressBookError);
if (couldSaveAddressBook){
NSLog(@"Successfully added the person to the group.");
result = YES;
} else {
NSLog(@"Failed to save the address book.");
}
} else {
NSLog(@"No changes were saved.");
}
return result;
}
#pragma mark - 查找通讯录
// 查找通讯录中联系人是否存在指定联系人
- (BOOL) doesPersonExistWithFirstName:(NSString *)paramFirstName lastName:(NSString *)paramLastName inAddressBook:(ABRecordRef)paramAddressBook{
BOOL result = NO;
if (paramAddressBook == NULL){
NSLog(@"The address book is null.");
return NO;
}
NSArray *allPeople = [(NSArray *) ABAddressBookCopyArrayOfAllPeople(paramAddressBook) autorelease];
NSUInteger peopleCounter = 0;
for (peopleCounter = 0; peopleCounter < [allPeople count]; peopleCounter++){
ABRecordRef person = (ABRecordRef)[allPeople objectAtIndex:peopleCounter];
NSString *firstName = [(NSString *) ABRecordCopyValue(person, kABPersonFirstNameProperty) autorelease];
NSString *lastName = [(NSString *) ABRecordCopyValue(person, kABPersonLastNameProperty) autorelease];
BOOL firstNameIsEqual = NO;
BOOL lastNameIsEqual = NO;
if ([firstName length] == 0 && [paramFirstName length] == 0){
firstNameIsEqual = YES;
}else if ([firstName isEqualToString:paramFirstName]){
firstNameIsEqual = YES;
}
if ([lastName length] == 0 &&[paramLastName length] == 0){
lastNameIsEqual = YES;
}else if ([lastName isEqualToString:paramLastName]){
lastNameIsEqual = YES;
}
if (firstNameIsEqual && lastNameIsEqual){
return YES;
}
}
return result;
}
// 查找通讯录中是否存在是某个分组
- (BOOL) doesGroupExistWithGroupName:(NSString *)paramGroupName inAddressBook:(ABAddressBookRef)paramAddressBook{
BOOL result = NO;
if (paramAddressBook == NULL){
NSLog(@"The address book is null.");
return NO;
}
NSArray *allGroups = [( NSArray *) ABAddressBookCopyArrayOfAllGroups(paramAddressBook) autorelease];
NSUInteger groupCounter = 0;
for (groupCounter = 0; groupCounter < [allGroups count];groupCounter++){
ABRecordRef group = ( ABRecordRef) [allGroups objectAtIndex:groupCounter];
NSString *groupName = [( NSString *) ABRecordCopyValue(group, kABGroupNameProperty) autorelease];
if ([groupName length] == 0 &&[paramGroupName length] == 0){
return YES;
}else if ([groupName isEqualToString:paramGroupName]){
return YES;
}
}
return result;
}
// 根据姓名,模糊查询
- (BOOL) doesPersonExistWithFullName:(NSString *)paramFullName inAddressBook:(ABAddressBookRef)paramAddressBook{
BOOL result = NO;
if (paramAddressBook == NULL){
NSLog(@"Address book is null.");
return NO;
}
NSArray *allPeopleWithThisName = [( NSArray *) ABAddressBookCopyPeopleWithName(paramAddressBook, (CFStringRef)paramFullName) autorelease];
if ([allPeopleWithThisName count] > 0){
result = YES;
}
return result;
}
#pragma mark - 通讯录图片相关
// 获得联系人的头像
- (UIImage *) getPersonImage:(ABRecordRef)paramPerson{ UIImage *result = nil;
if (paramPerson == NULL){
NSLog(@"The person is nil.");
return NULL;
}
NSData *imageData = [( NSData *) ABPersonCopyImageData(paramPerson) autorelease];
if (imageData != nil){
UIImage *image = [UIImage imageWithData:imageData];
result = image;
}
return result;
}
// 设置联系人的头像
- (BOOL) setPersonImage:(ABRecordRef)paramPerson inAddressBook:(ABAddressBookRef)paramAddressBook withImageData:(NSData *)paramImageData{
BOOL result = NO;
if (paramAddressBook == NULL){
NSLog(@"The address book is nil.");
return NO;
}
if (paramPerson == NULL){
NSLog(@"The person is nil.");
return NO;
}
CFErrorRef couldSetPersonImageError = NULL;
BOOL couldSetPersonImage = ABPersonSetImageData(paramPerson,(CFDataRef)paramImageData, &couldSetPersonImageError);
if (couldSetPersonImage){
NSLog(@"Successfully set the person's image. Saving...");
if (ABAddressBookHasUnsavedChanges(paramAddressBook)){
BOOL couldSaveAddressBook = NO;
CFErrorRef couldSaveAddressBookError = NULL;
couldSaveAddressBook = ABAddressBookSave(paramAddressBook, &couldSaveAddressBookError);
if (couldSaveAddressBook){
NSLog(@"Successfully saved the address book.");
result = YES;
} else {
NSLog(@"Failed to save the address book.");
}
} else {
NSLog(@"There are no changes to be saved!"); }
} else {
NSLog(@"Failed to set the person's image.");
}
return result;
}
iOS通讯录相关知识-浅析的更多相关文章
- iOS网络相关知识总结
iOS网络相关知识总结 1.关于请求NSURLRequest? 我们经常讲的GET/POST/PUT等请求是指我们要向服务器发出的NSMutableURLRequest的类型; 我们可以设置Reque ...
- 一文让你彻底了解iOS字体相关知识
写本文的契机主要是把自己整理的关于iOS字体方面的知识不断更新写在这篇博文中,用来自己以后查阅. 一.iOS原生字体展示 在label中选择字体的font,并把font由system改成custom后 ...
- iOS开发经验相关知识
一. iPhone Size 手机型号 屏幕尺寸 iPhone 4 4s 320 * 480 iPhone 5 5s 320 * 568 iPhone 6 6s 375 * 667 iphone 6 ...
- iOS网络相关零散知识总结
iOS网络相关零散知识总结 1. URL和HTTP知识 (1) URL的全称是Uniform Resource Locator(统一资源定位符). URL的基本格式 = 协议://主机地址/路径 ...
- 客户端相关知识学习(十二)之iOS H5交互Webview实现localStorage数据存储
前言 最近有一个需求是和在app中前端本地存储相关的,所以恶补了一下相关知识 webView开启支持H5 LocalStorage存储 有些时候我们发现写的本地存储没有起作用,那是因为默认WebVie ...
- iOS底层基础知识-文件目录结构
一:iOS沙盒知识 出于安全考虑,iOS系统把每个应用以及数据都放到一个沙盒(sandbox)里面,应用只能访问自己沙盒目录里面的文件.网络资源等(也有例外,比如系统通讯录.照相机.照片等能在用户授权 ...
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- UIViewController相关知识
title: UIViewController 相关知识date: 2015-12-13 11:50categories: IOS tags: UIViewController 小小程序猿我的博客:h ...
- iOS运行时Runtime浅析
运行时是iOS中一个很重要的概念,iOS运行过程中都会被转化为runtime的C代码执行.例如[target doSomething];会被转化成objc)msgSend(target,@select ...
随机推荐
- 钓鱼WIFI搭建
1.无线网卡 2.KaliLinux操作系统,这里就不用说了,必备的 3.isc-dhcp-server服务器.安装好KaliLinux后只需要apt-get update 然后apt-get i ...
- uboot下如何查看内存里的数据
答:使用md工具 md.b $address $count (从地址$address处显示$count个字节的数据,b=byte,8位) md.w $address $count (从地址$addre ...
- mybatis的一级缓存和二级缓存(1)
1.mybatis一级缓存,sqlSesion级别的缓存,一级缓存默认一直开启的,sqlSession级别的一个Map,把查询的数据放到一个Map中,以后需要相同的数据,直接从Map中去取 与数据库一 ...
- Hue的安装与部署
Hue的安装与部署 hadoop hue Hue 简介 Hue是一个开源的Apache Hadoop UI系统,最早是由Cloudera Desktop演化而来,由Cloudera贡献给开源社区,它是 ...
- Linux内核、 TCP/IP、Socket参数调优
/proc/sys/net目录 所有的TCP/IP参数都位于/proc/sys/net目录下(请注意,对/proc/sys/net目录下内容的修改都是临时的,任何修改在系统重启后都会丢失),例如下面这 ...
- u-boot-2015.07 autoconf.mk生成过程分析
1.u-boot2015.7版本编译没有在顶层目录中生成.config文件,而生成了include/autoconf.mk和include/autoconf.mk.dep两个文件,并在每个模块编译的时 ...
- 使用GEOquery下载GEO数据--转载
最近需要下载一大批GEO上的数据,问题是我要下载的Methylation数据根本就没有sra文件,换言之不能使用Aspera之类的数据进行下载.但是后来我发现了GEOquery这个不错的R包,不知道是 ...
- 记录一下我的mac的环境变量的配置参数
#配置jdk环境export JAVA_7_HOME=/Library/java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Homeexport JAV ...
- 设计模式--代理模式C++实现
代理模式C++实现 1定义 为其他对象提供一种代理以控制对这个对象的访问 2类图 角色定义: Subject抽象主体角色,抽象类或者接口,是一个普通的业务类型定义 RealSubject具体主体角色, ...
- 转:25个Java机器学习工具和库
转自:http://www.cnblogs.com/data2value/p/5419864.html 本列表总结了25个Java机器学习工具&库: 1. Weka集成了数据挖掘工作的机器学习 ...