标签:term mis string restrict oid 参数 smis -- sts
在iOS中,有2个框架可以访问用户的通讯录
AddressBookUI.framework
提供了联系人列表界面、联系人详情界面、添加联系人界面等
一般用于选择联系人
AddressBook.framework
纯C语言的API,仅仅是获得联系人数据 没有提供UI界面展示,需要自己搭建联系人展示界面 里面的数据类型大部分基于Core Foundation框架,使用起来极其蛋疼
从iOS6开始,需要得到用户的授权才能访问通讯录,因此在使用之前,需要检 查用户是否已经授权
获得通讯录的授权状态: ABAddressBookGetAuthorizationStatus()
? kABAuthorizationStatusRestricted
● iOS设备上的家长控制或其它一些许可配置阻止程序与通讯录数据库进行交互
? kABAuthorizationStatusDenied
● 用户明确的拒绝了你的程序对通讯录的访问
? kABAuthorizationStatusAuthorized
● 用户已经授权给你的程序对通讯录进行访问
granted, CFErrorRef error) {
if (granted) {
NSLog(@"授权成功!"); } else {
NSLog(@"授权失败!"); }
});
CFRelease(addressBook);
提示:申请通讯录访问授权的代码,通常放在AppDelegate中
● 联系人属性包括以下类型:
● 简单属性:姓、名等
● 多重属性:电话号码、电子邮件等
● 组合属性:地址等
● 注意:使用ABRecordCopyValue可以从一条Person记录中 获取到对应的记录,但是后续处理则需要根据记录的具体 类型加以区分
● 使用ABRecordCopyValue函数可以从ABRecordRef中获得联系人 的简单属性(例如:一个字符串)
● ABRecordCopyValue函数接收2个参数
● 第1个参数是ABRecordRef实例
● 第2个参数是属性关键字,定义在ABPerson.h中
● ABPersonCopyLocalizedPropertyName函数可以根据指定的关键 字获取对应的标签文本
NSInteger count = CFArrayGetCount(array);
for (NSInteger i = 0; i < count; ++i) {
// 取出一条记录
ABRecordRef person =CFArrayGetValueAtIndex(array, i);
// 取出个人记录中的详细信息
// 名
CFStringRef firstNameLabel =ABPersonCopyLocalizedPropertyName(kABPersonFirstNameProperty);
// 取出一条记录
ABRecordRef person = (__bridge ABRecordRef)(array[i]);
// 取出个人记录中的详细信息
NSString *firstNameLabel = (__bridge NSString *)
(ABPersonCopyLocalizedPropertyName(kABPersonFirstNameProperty));
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person,
kABPersonFirstNameProperty));
NSString *lastNameLabel = (__bridge NSString *)
(ABPersonCopyLocalizedPropertyName(kABPersonLastNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person,
kABPersonLastNameProperty));
NSLog(@"%@ %@ - %@ %@", lastNameLabel, lastName, firstNameLabel, firstName);
}
CFRelease(addressBook);
? 比如邮箱,分为工作邮箱、住宅邮箱、其他邮箱等
? 比如电话,分为工作电话、住宅电话、其他电话等
● 如果是复杂属性,那么ABRecordCopyValue函数返回的就 是ABMultiValueRef类型的数据,例如邮箱或者电话
// 取电话号码
ABMultiValueRef phones =ABRecordCopyValue(person,kABPersonPhoneProperty);
// 取记录数量
NSInteger phoneCount =ABMultiValueGetCount(phones);
// 电话标签
// 电话号码
CFStringRef phoneNumber =ABMultiValueCopyValueAtIndex(phones, i);
2.通过ABRecordSetValue函数设置联系人的属性
3.通过ABAddressBookAddRecord函数将联系人添加到通讯录数据 库中
4.通过ABAddressBookSave函数保存刚才所作的修改
● 可以通过ABAddressBookHasUnsavedChanges函数判断是否有未保存
的修改
● 当决定是否更改通讯录数据库后,你可以分别使用AbAddressBookSave或 ABAddressBookRevert 方式来保存或放弃更改
? 通过ABRecordSetValue函数设置组名
? 通过ABAddressBookAddRecord函数将组添加到通讯录数 据库中
? 通过ABAddressBookSave函数保存刚才所作的修改
● ABPersonCopyImageData
● 取得图片数据(假如有的话)
● ABPersonSetImageData
● 设置联系人的图片数据
实例1:使用AddressBookUI框架
// // HMViewController.m // 08-通讯录 // // Created by apple on 14-8-12. // Copyright (c) 2014年 heima. All rights reserved. // #import "HMViewController.h" #import <AddressBookUI/AddressBookUI.h> @interface HMViewController () <ABPeoplePickerNavigationControllerDelegate> @property (weak, nonatomic) IBOutlet UILabel *nameLabel; @property (weak, nonatomic) IBOutlet UILabel *phoneLabel; @end @implementation HMViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 弹出联系人列表 ABPeoplePickerNavigationController *ppnc = [[ABPeoplePickerNavigationController alloc] init]; ppnc.peoplePickerDelegate = self; [self presentViewController:ppnc animated:YES completion:nil]; } #pragma mark - ABPeoplePickerNavigationControllerDelegate /** * 当选中了某个联系人就会调用 * * @return NO : 不会继续执行下一个默认操作。 YES : 按照默认的做法 */ - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { // 设置联系人数据 // person中包括了姓名、电话属性 // copy / new / create / retain, 都需要在不使用的时候release NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)); NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty)); self.nameLabel.text = [NSString stringWithFormat:@"姓名:%@ %@", lastName, firstName]; // 获得电话号码 ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty); CFIndex phoneCount = ABMultiValueGetCount(phone); NSMutableString *phoneStr = [NSMutableString string]; [phoneStr appendString:@"电话:"]; for (int i = 0; i<phoneCount; i++) { NSString *label = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(phone, i)); NSString *value = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phone, i)); [phoneStr appendFormat:@"\n%@ : %@", label, value]; } self.phoneLabel.text = phoneStr; [peoplePicker dismissViewControllerAnimated:YES completion:nil]; CFRelease(phone); return NO; } /** * 当选中了联系人的某个属性时调用 * * @return NO : 不会继续执行下一个默认操作。 YES : 按照默认的做法 */ - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier { return NO; } /** * 点击了取消按钮 */ - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker { [peoplePicker dismissViewControllerAnimated:YES completion:nil]; } @end
实例2: 使用:AddressBook框架(要下载)
#import <AddressBook/AddressBook.h> @implementation HMAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 1.获得通讯录的授权状态 ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus(); if (status == kABAuthorizationStatusNotDetermined) { // 2.申请授权 ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); ABAddressBookRequestAccessWithCompletion(book, ^(bool granted, CFErrorRef error) { // 授权完毕后调用 if (granted == YES) { // 允许 NSLog(@"---允许"); } else { // 不允许 NSLog(@"---不允许"); } // 释放对象 CFRelease(book); }); } // Override point for customization after application launch. return YES; }
#import "HMViewController.h" #import <AddressBook/AddressBook.h> #import "AddressBook.h" @interface HMViewController () @end @implementation HMViewController - (void)viewDidLoad { [super viewDidLoad]; RHAddressBook *book = [[RHAddressBook alloc] init]; // [book addPerson:<#(RHPerson *)#>]; // [book addPerson:<#(RHPerson *)#>]; // [book addPerson:<#(RHPerson *)#>]; // [book save]; NSArray *people = [book people]; for (RHPerson *p in people) { NSLog(@"%@ %@", p.firstName, p.lastName); RHMultiValue *phones = p.phoneNumbers; int phoneCount = phones.count; for (int i = 0; i<phoneCount; i++) { NSString *label1 = [phones labelAtIndex:i]; NSString *label2 = [phones localizedLabelAtIndex:i]; NSString *value = phones.values[i]; NSLog(@"%@ %@ %@", label1, label2, value); } } // ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus(); // if (status != kABAuthorizationStatusAuthorized) return; // // // 1.获得通讯录对象 // ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL); // // // 2.访问通讯录中的所有联系人 // CFArrayRef peopleArray = ABAddressBookCopyArrayOfAllPeople(book); // CFIndex count = CFArrayGetCount(peopleArray); // for (CFIndex i = 0; i<count; i++) { // // 取出i位置的联系人 // ABRecordRef people = CFArrayGetValueAtIndex(peopleArray, i); // // // 获得姓名 // CFStringRef firstName = ABRecordCopyValue(people, kABPersonFirstNameProperty); // CFStringRef lastName = ABRecordCopyValue(people, kABPersonLastNameProperty); // CFStringRef org = ABRecordCopyValue(people, kABPersonOrganizationProperty); // // // NSLog(@"%@ %@ %@", firstName, lastName, org); // // 获得电话 // ABMultiValueRef phone = ABRecordCopyValue(people, kABPersonPhoneProperty); // CFIndex phoneCount = ABMultiValueGetCount(phone); // for (CFIndex j = 0; j<phoneCount; j++) { // CFStringRef phoneValue = ABMultiValueCopyValueAtIndex(phone, j); // CFStringRef phoneLabel = ABMultiValueCopyLabelAtIndex(phone, j); // // NSLog(@"%@ %@", phoneLabel, phoneValue); // // CFRelease(phoneLabel); // CFRelease(phoneValue); // } // CFRelease(firstName); // CFRelease(lastName); // CFRelease(org); // } // // // 3.不需要通讯录 // CFRelease(peopleArray); // CFRelease(book); } @end
标签:term mis string restrict oid 参数 smis -- sts
原文地址:http://www.cnblogs.com/liuwj/p/6878399.html