AddressBook 调用系统通讯录数据
来源:互联网 发布:sql group by的用法 编辑:程序博客网 时间:2024/06/05 00:57
iOS6.0之后需要首先在AppDelegate里进行授权
@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL); //1.获取授权状态 ABAuthorizationStatus type = ABAddressBookGetAuthorizationStatus(); //授权申请 if (type == kABAuthorizationStatusNotDetermined) { ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { if (granted) { NSLog(@"授权允许"); }else{ NSLog(@"授权拒绝"); return; } }); } return YES;}@end
授权的状态:
kABAuthorizationStatusNotDetermined 用户还没有决定
kABAuthorizationStatusRestricted, 受限制
kABAuthorizationStatusDenied, 拒绝
kABAuthorizationStatusAuthorized 许可
以下代码为MRC模式下运行,如果使用ARC,系统会自动提示你在需要的位置添加__bridge_ 桥接转换
// 实例化通讯录对象 ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL); // 获取所有联系人记录 CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook); /* 联系人属性包括以下类型: 简单属性:姓、名等 多重属性:电话号码、电子邮件等 组合属性:地址等 */ for(int i = 0; i < CFArrayGetCount(results); i++) { /* 一个联系人就是一个ABRecordRef,每个联系人都有自己的属性,比如名字、电话、邮件等 使用ABRecordCopyValue函数可以从ABRecordRef中获得联系人的简单属性 ABRecordCopyValue函数接收2个参数 第1个参数是ABRecordRef实例 第2个参数是属性关键字,定义在ABPerson.h中 */ #pragma mark - 简单属性 ABRecordRef person = CFArrayGetValueAtIndex(results, i); //读取firstname NSString *personName = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty); if(personName != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"\n姓名:%@\n",personName]; //读取lastname NSString *lastname = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty); if(lastname != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@\n",lastname]; //读取middlename NSString *middlename = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty); if(middlename != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@\n",middlename]; //读取prefix前缀 NSString *prefix = (NSString*)ABRecordCopyValue(person, kABPersonPrefixProperty); if(prefix != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@\n",prefix]; //读取suffix后缀 NSString *suffix = (NSString*)ABRecordCopyValue(person, kABPersonSuffixProperty); if(suffix != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@\n",suffix]; //读取nickname呢称 NSString *nickname = (NSString*)ABRecordCopyValue(person, kABPersonNicknameProperty); if(nickname != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@\n",nickname]; //读取firstname拼音音标 NSString *firstnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty); if(firstnamePhonetic != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@\n",firstnamePhonetic]; //读取lastname拼音音标 NSString *lastnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty); if(lastnamePhonetic != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@\n",lastnamePhonetic]; //读取middlename拼音音标 NSString *middlenamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty); if(middlenamePhonetic != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@\n",middlenamePhonetic]; //读取organization公司 NSString *organization = (NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty); if(organization != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@\n",organization]; //读取jobtitle工作 NSString *jobtitle = (NSString*)ABRecordCopyValue(person, kABPersonJobTitleProperty); if(jobtitle != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@\n",jobtitle]; //读取department部门 NSString *department = (NSString*)ABRecordCopyValue(person, kABPersonDepartmentProperty); if(department != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@\n",department]; //读取birthday生日 NSDate *birthday = (NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty); if(birthday != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@\n",birthday]; #pragma mark - 多值属性 //获取email多值 ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty); int emailcount = ABMultiValueGetCount(email); for (int x = 0; x < emailcount; x++) { //获取email Label NSString* emailLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(email, x)); //获取email值 NSString* emailContent = (NSString*)ABMultiValueCopyValueAtIndex(email, x); cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@:%@\n",emailLabel,emailContent]; } //读取地址多值 ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty); int count = ABMultiValueGetCount(address); for(int j = 0; j < count; j++) { //获取地址Label(工作/住宅) NSString* addressLabel = (NSString*)ABMultiValueCopyLabelAtIndex(address, j); cell.textView.text = [cell.textView.text stringByAppendingFormat:@"++%@++\n",addressLabel]; //获取該label下的地址6属性 NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(address, j); NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey]; if(country != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"国家:%@\n",country]; NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey]; if(city != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"城市:%@\n",city]; NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey]; if(state != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"省:%@\n",state]; NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey]; if(street != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"街道:%@\n",street]; NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey]; if(zip != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"邮编:%@\n",zip]; NSString* coutntrycode = [personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey]; if(coutntrycode != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"国家编号:%@\n",coutntrycode]; } //获取dates多值 ABMultiValueRef dates = ABRecordCopyValue(person, kABPersonDateProperty); int datescount = ABMultiValueGetCount(dates); for (int y = 0; y < datescount; y++) { //获取dates Label NSString* datesLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(dates, y)); //获取dates值 NSString* datesContent = (NSString*)ABMultiValueCopyValueAtIndex(dates, y); cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@:%@\n",datesLabel,datesContent]; } // //获取kind值 // CFNumberRef recordType = ABRecordCopyValue(person, kABPersonKindProperty); // if (recordType == kABPersonKindOrganization) { // // it's a company // NSLog(@"it's a company\n"); // } else { // // it's a person, resource, or room // NSLog(@"it's a person, resource, or room\n"); // } //获取IM多值 ABMultiValueRef instantMessage = ABRecordCopyValue(person, kABPersonInstantMessageProperty); for (int l = 1; l < ABMultiValueGetCount(instantMessage); l++) { //获取IM Label NSString* instantMessageLabel = (NSString*)ABMultiValueCopyLabelAtIndex(instantMessage, l); cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@\n",instantMessageLabel]; //获取該label下的2属性 NSDictionary* instantMessageContent =(NSDictionary*) ABMultiValueCopyValueAtIndex(instantMessage, l); NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey]; if(username != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"username:%@\n",username]; NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey]; if(service != nil) cell.textView.text = [cell.textView.text stringByAppendingFormat:@"service:%@\n",service]; } //读取电话多值 ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty); for (int k = 0; k<ABMultiValueGetCount(phone); k++) { //获取电话Label NSString * personPhoneLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k)); //获取該Label下的电话值 NSString * personPhone = (NSString*)ABMultiValueCopyValueAtIndex(phone, k); cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@:%@\n",personPhoneLabel,personPhone]; } //获取URL多值 ABMultiValueRef url = ABRecordCopyValue(person, kABPersonURLProperty); for (int m = 0; m < ABMultiValueGetCount(url); m++) { //获取电话Label NSString * urlLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(url, m)); //获取該Label下的电话值 NSString * urlContent = (NSString*)ABMultiValueCopyValueAtIndex(url,m); cell.textView.text = [cell.textView.text stringByAppendingFormat:@"%@:%@\n",urlLabel,urlContent]; } // //读取照片 // NSData *image = (NSData*)ABPersonCopyImageData(person); // // UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(200, 0, 50, 50)]; // [myImage setImage:[UIImage imageWithData:image]]; // myImage.opaque = YES; // [cell.textView addSubview:myImage]; NSLog(@"\n\n%@\n--------------------------------------------------------\n\n",cell.textView.text); } _autoHeight = [HeightForCell heightFoTextLabel:cell.textView.text width:cell.contentView.frame.size.width fontSize:18]; cell.textView.frame = CGRectMake(0, 0, cell.frame.size.width, _autoHeight); CFRelease(results); CFRelease(addressBook);
kABPersonFirstNameProperty; // 名字
kABPersonLastNameProperty; // 姓氏
kABPersonMiddleNameProperty; // 中间名
kABPersonPrefixProperty; // 前缀
kABPersonSuffixProperty; // 后缀
kABPersonNicknameProperty; // 昵称
kABPersonFirstNamePhoneticProperty; // 名字的汉语拼音或者音标
kABPersonLastNamePhoneticProperty; // 姓氏汉语拼音或者音标
kABPersonMiddleNamePhoneticProperty; // 中间名的汉语拼音或者音标
kABPersonOrganizationProperty; // 组织名
kABPersonJobTitleProperty; // 工作头衔
kABPersonDepartmentProperty; // 部门
kABPersonNoteProperty; // 备注
kABPersonBirthdayProperty; // 生日
kABPersonCreationDateProperty; // 创建时间
kABPersonModificationDateProperty; // 修改日期</pre><pre class="brush:java;"></pre><pre class="brush:java;"> //多值属性 (一个属性中又多个值) 有标签、值、id
kABPersonPhoneProperty ; //电话号码属性 kABMultiStringPropertyType;//类型
kABPersonEmailProperty ; //e-mail属性 kABMultiStringPropertyType;//类型
kABPersonURLProperty ; //URL属性 kABMultiStringPropertyType;//类型
kABPersonRelatedNamesProperty; // 亲属关系人属性 kABMultiStringPropertyType;//类型
kABPersonAddressProperty ; //地址属性 kABMultiDictionaryPropertyType;//类型
kABPersonInstantMessageProperty;//及时聊天属性 kABMultiDictionaryPropertyType;//类型
kABPersonSocialProfileProperty; //社交账号属性 kABMultiDictionaryPropertyTypel;//类型</pre><br>利用上面的属性来查找想要的值
0 0
- AddressBook 调用系统通讯录数据
- 通讯录AddressBook
- 调用android系统通讯录 添加一条通讯录数据
- 通讯录(AddressBook)1
- 通讯录(AddressBook)2
- 通讯录(AddressBook)3
- AddressBook获取通讯录
- android调用系统通讯录
- 调用系统通讯录
- 调用系统通讯录
- ios调用系统通讯录
- ios 调用系统通讯录
- 调用系统通讯录
- Android 调用系统通讯录
- 手机通讯录AddressBook的使用
- AddressBook通讯录右边索引条
- ios通讯录------AddressBook基本使用
- 调用系统通讯录,返回结果
- JS+CSS简单实现DIV遮罩层显示隐藏
- 1063:字符串匹配问题
- Windows中导出电脑文件名或者文件结构的方法
- PCB流程及经验简述
- PopupWindow$PopupViewContainer.dispatchKeyEvent
- AddressBook 调用系统通讯录数据
- Mysql数据表空间计算方法
- 将上拉加载更多下拉刷新PullToRefreshView用到项目中去
- 软键盘弹出不影响布局的方法
- 文字抛光特效
- 标准模板库(STL)使用---vector
- c#采集百度图片出现403 Forbidden 的解决办法
- 简单工厂模式
- Android软键盘回车改为搜索