AddressBook 调用系统通讯录数据

4 篇文章 0 订阅
1 篇文章 0 订阅

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>利用上面的属性来查找想要的值


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值