AddressBook 电话簿访问与修改

访问地址簿和单个联系人数据的接口是基于C语言的函数,接口传递对地址簿各种对象的引用作为参数。管理地址簿中条目的基类对象是 ABRecord。一个 ABRecord 可以表示一个人 或者一个群体 ABGroup 。无论何时,在界面上选中一条记录,或者使用框架进行查询返回一条结果,系统都会返回一个指向 ABRecord 的指针,标示为 ABRecordRef。与地址簿 API 的大部分交互都涉及使用 ABRecordRef 这个引用类型。


常用访问记录的函数如下:


ABRecordID ABRecordGetRecord(ABRecordRef record);


返回 ABRecordID ,代表了 记录在底层数据库中的ID号。具有唯一性。
ABRecordType ABRecordGetRecordType(ABRecordRef record);


返回记录类型。可以是 kABPersonType 和 kABGroupType
CFStringRef ABRecordCopyCompositeName(ABRecordRef record);


返回个人或群体完整名称。例:NSString* name = (NSString*)ABRecordCopyCompositeName(record);

一、高层地址簿函数

1.获得地址簿句柄

你必须先初始化地址簿,然后才能对其进读写操作。要获得一个地址簿句柄,可以使用 ABAddressBookCreate函数:

#import <AddressBook/AddressBook.h>  
AddressBookRef ab = AddressBookCreate();  
2. 保存地址簿


获得了地址簿的引用就可以对其进行操作了,操作完毕要记得保存:


CFErrorRef err;  
BOOL success =  ABAddressBookSave(ab, &err);  
如果不确定是否需要保存,则可以用:


BOOL hasUnsavedChanges = ABAddressBookHasUnsavedChanges(ab);  
3.添加/删除 记录

CFErrorRef err;  
BOOL success =ABAddressBookAddRecord(ab, record, &err);//添加  

CFErrorRef err;  
BOOL success =ABAddressBookRemoveRecord(ab, record, &err);//删除  

二、查询地址簿
 地址簿框架仅仅提供了基本的查询功能。可以用函数根据名字来查询多个记录,或者是根据特定记录 ID 查询单个记录。

1.获取地址簿中记录总个数:




CFIndex count = ABAddressBookGetPersonCount(ab);  
    printf("%ld total entries in the address book\n",count);  
2. 获取所有联系人:


NSArray* array = (NSArray*)ABAddressBookCopyArrayOfAllPeople(ab);  
   printf("Retrieved %d contacts\n",[array count]);  
3. 在联系人列表中查询一个特定的名字:

NSArray* arrayByName = (NSArray*)ABAddressBookCopyPeopleWithName(ab, CFSTR("Liu Wei"));  
函数如其名,这个函数返回的并不是地址簿中实际的对象,而是拷贝。要访问这个数组的单个记录,就用NSArray的方法啦:

ABRecordRef myRecord = [arrayByName objectAtIndex:0];  
除了通过名字查询,你也可以直接通过ID查询(假如你知道ID的话):

ABRecordRef myRecord = ABAddressBookGetPersonWithRecordID(ab, recordID);  
三、创建记录

创建新的联系人可以使用 ABPersonCreate 函数。这样可以得到一个空记录,然后就可以向其中填充信息:

ABRecordRef record = ABPersonCreate();  
四、操纵记录
  一旦获得了 ABRecordRef,就可以确定是属于个人还是群体,也就可以访问更进一步的信息了。姓名以及其他各项信息都可以通过相应属性来操纵。只有ABPerson 类型实体记录才会有属性,而每个记录都具有各种各样的信息。

查询给定记录的信息,可以使用 ABRecordCopyValue 函数。这个函数原型如下:

CFTypeRef ABRecordCopyValue(ABRecordRef record,kABPersonFirstNameProperty);  
调用这个函数时,会将你指定 的属性拷贝一份,并返回引用:


CFStringRef firstName = ABRecordCopyValue(record, kABPersonFirstNameProperty);  
由于kABPersonFirstNameProperty 属性是一个 CFString,你可以将其转换为 NSString* :

NSString* firstName =NSString* ABRecordCopyValue(record, kABPersonFirstNameProperty);  
就像CFStringRef 可以转换为 NSString* 一样,如果有属性返回类型是CFDateRef,你也可以转换为 NSDate* :

NSDate* birthday =  (NSDate*) ABRecordCopyValue(record, kABPersonBirthdayProperty);  
上面所指定的 ABPropertyID, 是一个与在记录中查找信息相对应的值。根据这个值返回ABPerson 对象属性。由于 ABRecordCopyValue 函数返回的数据类型是通用的 CFTypeRef ,所以结果可疑被转换为一个与属性相对应的更细化的数据类型,如下表:

// Property keys  
extern const ABPropertyID kABPersonFirstNameProperty;          // First name - kABStringPropertyType  
extern const ABPropertyID kABPersonLastNameProperty;           // Last name - kABStringPropertyType  
extern const ABPropertyID kABPersonMiddleNameProperty;         // Middle name - kABStringPropertyType  
extern const ABPropertyID kABPersonPrefixProperty;             // Prefix ("Sir" "Duke" "General") - kABStringPropertyType  
extern const ABPropertyID kABPersonSuffixProperty;             // Suffix ("Jr." "Sr." "III") - kABStringPropertyType  
extern const ABPropertyID kABPersonNicknameProperty;           // Nickname - kABStringPropertyType  
extern const ABPropertyID kABPersonFirstNamePhoneticProperty;  // First name Phonetic - kABStringPropertyType  
extern const ABPropertyID kABPersonLastNamePhoneticProperty;   // Last name Phonetic - kABStringPropertyType  
extern const ABPropertyID kABPersonMiddleNamePhoneticProperty; // Middle name Phonetic - kABStringPropertyType  
extern const ABPropertyID kABPersonOrganizationProperty;       // Company name - kABStringPropertyType  
extern const ABPropertyID kABPersonJobTitleProperty;           // Job Title - kABStringPropertyType  
extern const ABPropertyID kABPersonDepartmentProperty;         // Department name - kABStringPropertyType  
extern const ABPropertyID kABPersonEmailProperty;              // Email(s) - kABMultiStringPropertyType  
extern const ABPropertyID kABPersonBirthdayProperty;           // Birthday associated with this person - kABDateTimePropertyType  
extern const ABPropertyID kABPersonNoteProperty;               // Note - kABStringPropertyType  
extern const ABPropertyID kABPersonCreationDateProperty;       // Creation Date (when first saved)  
extern const ABPropertyID kABPersonModificationDateProperty;   // Last saved date  
五、写入属性


向记录写入属性,使用ABRecordSetValue 函数:




CFStringRef name = CFSTR("IUKEY");  
    BOOL suc = ABRecordSetValue(record, kABPersonNicknameProperty, name, &err);  
    if (suc) {  
        NSLog(@"setValue succeed");  
    }  
删除属性:

BOOL succeed = ABRecordRemoveValue(record, kABPersonFirstNameProperty, &err);  
当修改完以后不要忘记保存地址簿。
六、多值属性

除了前面列出的属性之外,一个记录还可能会有一些属性,其中包含多个值。多只属性可以用一种索引机制来处理,使用时首先查询值的总数,然后通过特定的索引得到一个条目。指向多值数据的指针,可以首先通过前面提到的 ABRecordCopyValue 方法得到,然后转换成 MultiValueRef :

ABMultiValueRef phoneNumbers = ABRecordCopyValue(record, kABPersonPhoneProperty);  
然后你可以通过这个引用来确定值的个数,并按照索引获取其中的单个值。函数ABMultiGetCount 可以返回条目个数,按照索引拷贝指定条目,可以用ABMultiValueCopyValueAtIndex 函数。
下面列出了多值属性中的条目:


extern const ABPropertyID kABPersonEmailProperty;  
extern const ABPropertyID kABPersonAddressProperty;  
extern const ABPropertyID kABPersonDateProperty;   
extern const ABPropertyID kABPersonPhoneProperty;  
extern const ABPropertyID kABPersonInstantMessageProperty;   
extern const ABPropertyID kABPersonURLProperty;   
除了多值属性中真正的值,每个条目还有一个标签。标签描述了返回的条目的类型。例如,一个电话号码标签有可能指明了这个号码是家庭电话还是手机号。地址的标签则可一描述是家庭地址还是工作地址。要查询特定条目标签,可以使用 ABMultValueCopyLabelAtIndex 函数:

CFStringRef label = ABMultiValueCopyLabelAtIndex(phoneNumbers, i);  
默写属性具有一组预定义的标签。下面这些 CFStringRef 类型标签的原型是在 ABPerson.h 中指定的:

extern const ABPropertyID kABPersonDateProperty;               // Dates associated with this person - kABMultiDatePropertyType  
extern const CFStringRef kABPersonAnniversaryLabel;  

// Phone numbers  
extern const ABPropertyID kABPersonPhoneProperty;              // Generic phone number - kABMultiStringPropertyType  
extern const CFStringRef kABPersonPhoneMobileLabel;  
extern const CFStringRef kABPersonPhoneIPhoneLabel __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);  
extern const CFStringRef kABPersonPhoneMainLabel;  
extern const CFStringRef kABPersonPhoneHomeFAXLabel;  
extern const CFStringRef kABPersonPhoneWorkFAXLabel;  
extern const CFStringRef kABPersonPhoneOtherFAXLabel __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);  
extern const CFStringRef kABPersonPhonePagerLabel;  

// IM  
extern const ABPropertyID kABPersonInstantMessageProperty;     // Instant Messaging - kABMultiDictionaryPropertyType  
extern const CFStringRef kABPersonInstantMessageServiceKey;     // Service ("Yahoo", "Jabber", etc.)  
extern const CFStringRef kABPersonInstantMessageServiceYahoo;  
extern const CFStringRef kABPersonInstantMessageServiceJabber;  
extern const CFStringRef kABPersonInstantMessageServiceMSN;  
extern const CFStringRef kABPersonInstantMessageServiceICQ;  
extern const CFStringRef kABPersonInstantMessageServiceAIM;  

// URLs  
extern const ABPropertyID kABPersonURLProperty;                // URL - kABMultiStringPropertyType  
extern const CFStringRef kABPersonHomePageLabel; // Home Page  

// Related names  
extern const ABPropertyID kABPersonRelatedNamesProperty;       // Names - kABMultiStringPropertyType  
extern const CFStringRef kABPersonFatherLabel;    // Father  
extern const CFStringRef kABPersonMotherLabel;    // Mother  
extern const CFStringRef kABPersonParentLabel;    // Parent  
extern const CFStringRef kABPersonBrotherLabel;   // Brother  
extern const CFStringRef kABPersonSisterLabel;    // Sister  
extern const CFStringRef kABPersonChildLabel;      // Child  
extern const CFStringRef kABPersonFriendLabel;    // Friend  
extern const CFStringRef kABPersonSpouseLabel;    // Spouse  
extern const CFStringRef kABPersonPartnerLabel;   // Partner  
extern const CFStringRef kABPersonAssistantLabel; // Assistant  
extern const CFStringRef kABPersonManagerLabel;   // Manager  
许多属性使用一组通用标签,标识工作、家庭以及其它地点。这些通用的标签如下:
kABWorkLabel


kABHomeLabel


kABOtherLabel

写入多只属性条目:

为了在现有的属性中加入一个值,你必须首先从记录中复制出多值字典。然后 ABMultiValueAddValueAndLabel 函数操作拷贝,将新的值-标签加入到字典中。最后用函数 ABRecordSetValue 将字典条目写回到地址簿记录中,完全替换掉整个多值属性。

七、使用字典

地址簿记录用字典来 表示地址和即时通讯账号。这些字典是内嵌在多值属性条目之中的。要访问这些字典,需要将值复制出来,并转换成 NSDictionary* 。然后你就可以用一组预定于的键值来访问字典了。

八、图像数据

某些联系人可能会有与之相关联的图像。可以使用 ABPersonCopyImageData 函数获取这些图像数据,返回将是一个 CFDataRef。可以把它转换成 NSData*,然后用来初始化一个UIImage对象。

if (ABPersonHasImageData(record)) {  
       UIImage *addressVookImage = [UIImage imageWithData:(NSData*)ABPersonCopyImageData(record)];  
   }  

九、地址簿界面


 地址簿界面框架提供了两种关键的用户界面:一个“找人”导航控件,来选择联系人;以及一个视图控件,用于显示单个联系人。

1. 联系人视图

ABPersonViewController 提供了一个简单的界面,可以向用户显示一个联系人。联系人视图需要一个 CFRecordRef 。

ABPersonViewController *viewController = [[ABPersonViewController alloc]init];  
可以将希望显示的记录赋值给 displayedPerson 属性:

viewController.displayedPerson=record;  
然后,你可以创建一个数组,其中包含你想要显示给用户的属性。只有指定的属性才会被显示出来,不过如果联系人被修改了,所有属性都将显示出来。可用的属性值与前面提到的枚举值相同。每个都作为一个 NSNumber 对象加入到数组中。


NSMutableArray* properties = [[NSMutableArray alloc]init];  
[properties addObject:[NSNumber numberWithInt:kABPersonFirstNameProperty ]];  
[properties addObject:[NSNumber numberWithInt:kABPersonLastNameProperty ]];  
[properties addObject:[NSNumber numberWithInt:kABPersonOrganizationProperty ]];  
viewController.allowsEditing =YES;  
十、联系人选取器
如果应用程序要选取一系列联系人,ABPeoplePickerNavigationController 类正好适合你。这个导航控件 可以显示联系人,让用户从中选择其一。选好后,你可以选择向用户显示该联系人,也可以通过一个委托方法实现你自己的行为。

ABPeoplePickerNavigationController* peoplePicker = [[ABPeoplePickerNavigationController alloc]init];  
如果希望允许用户查看单个联系人,你可以赋予其一组希望用户看到的属性。默认情况下会向用户显示所有的项目。 可用的属性值就是本章前面讲授的那些枚举值。每个作为一个NSNumber对象加入到数组中:

NSMutableArray* properties = [[NSMutableArray alloc]init];  
   [properties addObject:[NSNumber numberWithInt:kABPersonFirstNameProperty ]];  
   [properties addObject:[NSNumber numberWithInt:kABPersonLastNameProperty ]];  
   [properties addObject:[NSNumber numberWithInt:kABPersonOrganizationProperty ]];  
   peoplePicker.displayedProperties = properties;  
可以指定一个委托,在用户选定联系人时接收通知:
peoplePicker.peoplePickerDelegate =self;  

[self.view addSubview:peoplePicker.view];  

委托方发:

-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{  
    //取消选择  
}  
  
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{  
    //选中联系人  
      
}  
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{  
      
    //选中属性  
}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值