`

获得通讯录中联系人的所有属性ABAddressBookRef

阅读更多
获得通讯录中联系人的所有属性  ,看代码:


    ABAddressBookRef addressBook = ABAddressBookCreate();

    CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook);
   
    for(int i = 0; i < CFArrayGetCount(results); i++)
    {
        ABRecordRef person = CFArrayGetValueAtIndex(results, i);
        //读取firstname
        NSString *personName = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        if(personName != nil)
            textView.text = [textView.text stringByAppendingFormat:@"\n姓名:%@\n",personName];
        //读取lastname
        NSString *lastname = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
        if(lastname != nil)
            textView.text = [textView.text stringByAppendingFormat:@"%@\n",lastname];
        //读取middlename
        NSString *middlename = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);
        if(middlename != nil)
            textView.text = [textView.text stringByAppendingFormat:@"%@\n",middlename];
        //读取prefix前缀
        NSString *prefix = (NSString*)ABRecordCopyValue(person, kABPersonPrefixProperty);
        if(prefix != nil)
            textView.text = [textView.text stringByAppendingFormat:@"%@\n",prefix];
        //读取suffix后缀
        NSString *suffix = (NSString*)ABRecordCopyValue(person, kABPersonSuffixProperty);
        if(suffix != nil)
            textView.text = [textView.text stringByAppendingFormat:@"%@\n",suffix];
        //读取nickname呢称
        NSString *nickname = (NSString*)ABRecordCopyValue(person, kABPersonNicknameProperty);
        if(nickname != nil)
            textView.text = [textView.text stringByAppendingFormat:@"%@\n",nickname];
        //读取firstname拼音音标
        NSString *firstnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty);
        if(firstnamePhonetic != nil)
            textView.text = [textView.text stringByAppendingFormat:@"%@\n",firstnamePhonetic];
        //读取lastname拼音音标
        NSString *lastnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty);
        if(lastnamePhonetic != nil)
            textView.text = [textView.text stringByAppendingFormat:@"%@\n",lastnamePhonetic];
        //读取middlename拼音音标
        NSString *middlenamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty);
        if(middlenamePhonetic != nil)
            textView.text = [textView.text stringByAppendingFormat:@"%@\n",middlenamePhonetic];
        //读取organization公司
        NSString *organization = (NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);
        if(organization != nil)
            textView.text = [textView.text stringByAppendingFormat:@"%@\n",organization];
        //读取jobtitle工作
        NSString *jobtitle = (NSString*)ABRecordCopyValue(person, kABPersonJobTitleProperty);
        if(jobtitle != nil)
            textView.text = [textView.text stringByAppendingFormat:@"%@\n",jobtitle];
        //读取department部门
        NSString *department = (NSString*)ABRecordCopyValue(person, kABPersonDepartmentProperty);
        if(department != nil)
            textView.text = [textView.text stringByAppendingFormat:@"%@\n",department];
        //读取birthday生日
        NSDate *birthday = (NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty);
        if(birthday != nil)
            textView.text = [textView.text stringByAppendingFormat:@"%@\n",birthday];
        //读取note备忘录
        NSString *note = (NSString*)ABRecordCopyValue(person, kABPersonNoteProperty);
        if(note != nil)
            textView.text = [textView.text stringByAppendingFormat:@"%@\n",note];
        //第一次添加该条记录的时间
        NSString *firstknow = (NSString*)ABRecordCopyValue(person, kABPersonCreationDateProperty);
        NSLog(@"第一次添加该条记录的时间%@\n",firstknow);
        //最后一次修改該条记录的时间
        NSString *lastknow = (NSString*)ABRecordCopyValue(person, kABPersonModificationDateProperty);
        NSLog(@"最后一次修改該条记录的时间%@\n",lastknow);
       
        //获取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);
            textView.text = [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);
            textView.text = [textView.text stringByAppendingFormat:@"%@\n",addressLabel];
            //获取該label下的地址6属性
            NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(address, j);       
            NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];
            if(country != nil)
                textView.text = [textView.text stringByAppendingFormat:@"国家:%@\n",country];
            NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];
            if(city != nil)
                textView.text = [textView.text stringByAppendingFormat:@"城市:%@\n",city];
            NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];
            if(state != nil)
                textView.text = [textView.text stringByAppendingFormat:@"省:%@\n",state];
            NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];
            if(street != nil)
                textView.text = [textView.text stringByAppendingFormat:@"街道:%@\n",street];
            NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey];
            if(zip != nil)
                textView.text = [textView.text stringByAppendingFormat:@"邮编:%@\n",zip];   
            NSString* coutntrycode = [personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey];
            if(coutntrycode != nil)
                textView.text = [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);
            textView.text = [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);
            textView.text = [textView.text stringByAppendingFormat:@"%@\n",instantMessageLabel];
            //获取該label下的2属性
            NSDictionary* instantMessageContent =(NSDictionary*) ABMultiValueCopyValueAtIndex(instantMessage, l);       
            NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey];
            if(username != nil)
                textView.text = [textView.text stringByAppendingFormat:@"username:%@\n",username];
           
            NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey];
            if(service != nil)
                textView.text = [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);
               
            textView.text = [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);
           
            textView.text = [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;
        [textView addSubview:myImage];
       

   
    }
   
    CFRelease(results);
    CFRelease(addressBook);

原帖地址:http://www.cocoachina.com/bbs/read.php?tid=62527&fpage=2
分享到:
评论

相关推荐

    iOS通讯录的demo

    这个框架的核心类包括`ABAddressBookRef`,它是整个通讯录的容器,`ABRecordRef`代表一个具体的联系人或群组,而`ABPropertyID`定义了联系人的各个属性,如姓名、电话号码和电子邮件地址。 在开始使用AddressBook...

    ios 获取通讯录的代码

    在iOS平台上,获取通讯录是开发过程中常见的需求,主要用于实现如联系人同步、备份或特定功能的个性化。本文将详细讲解如何使用Objective-C或Swift语言来获取iOS设备上的通讯录,并将单个联系人信息存储到数组中。 ...

    iOS两种获取通讯录方式

    可以指定需要获取的联系人属性,如姓名、电话号码等。 5. 处理结果:这些方法会返回一个`Operation`对象,需要在完成处理时进行操作。通常会在`completionHandler`中处理结果。 第二种方式是使用`AddressBook ...

    ios-通讯录.zip

    9. **联系人属性**: 除了基本的姓名和电话,AddressBook还支持许多其他属性,如生日、地址、公司、职位等。每个属性都有对应的常量,如`kABPersonAddressProperty`用于地址,`kABPersonNoteProperty`用于备注。 ...

    ios-iOS9以下使用通讯录获取名称和电话号码的一个小例子.zip

    注意,`kABPersonPhoneProperty`是多值属性,意味着一个联系人可能有多个电话号码,因此需要使用`ABMultiValueCopyValueAtIndex`来获取每个电话号码。 在实际开发中,为了适应iOS9及以上版本,你需要使用`Contacts`...

    IOS 通讯录

    在iOS系统中,通讯录是用户存储联系人信息的重要组成部分,它允许用户管理他们的联系人、电话号码、电子邮件地址等。iOS提供了强大的API,即Contacts框架(在iOS 9及更高版本中)和旧版的AddressBook框架(适用于iOS...

    IOS中获取本地通讯录联系人以及汉字首字母排序

    在iOS平台上,获取本地通讯录联系人以及进行汉字首字母排序是常见的功能需求,尤其对于开发涉及用户通讯录的应用来说。下面将详细讲解这个过程,包括如何获取通讯录权限、读取联系人信息以及实现汉字首字母排序。 ...

    ios 读取通讯录 支持ios6.0

    总之,iOS中的通讯录访问涉及到用户授权、获取联系人列表、处理联系人属性等步骤。通过这个DEMO,你可以学习到如何读取和显示用户的姓名和手机号,同时也可以扩展到获取其他字段,如邮箱、地址等。在实际应用中,还...

    iOS获取通讯录

    总之,iOS获取通讯录涉及到AddressBook框架、权限管理以及对联系人属性的访问。通过这些技术,开发者可以为用户提供如查看、管理通讯录等服务,但必须始终尊重并保护用户的隐私。在实际开发中,应遵守Apple的App ...

    iphone 手机通讯录有关访问

    例如,创建一个新的联系人并设置一些属性: ```objective-c // 创建ABMutablePerson对象 ABMutablePersonRef newPerson = ABPersonCreate(); // 设置联系人的名字 CFStringRef firstName = CFSTR("John"); ...

    iOS通讯录访问操作封装,全部封装为objective-c对象,不用再使用底层的C语言操作通讯录了.zip

    1. `SLAddressBook.h`: 这是头文件,定义了类的接口和公共方法,如初始化通讯录存储、获取所有联系人、根据关键词搜索联系人等。 2. `SLAddressBook.m`: 这是实现文件,包含了类的方法实现,如与系统通讯录框架交互...

    ios-AddressBookTest.zip

    这个"ios-AddressBookTest.zip"项目显然关注了如何在iOS8及更高版本中处理联系人数据,尤其是如何将AddressBook对象转换为Objective-C对象,并且在iOS9中使用新的CNContact框架来读取和写入联系人属性。下面我们将...

    详解iOS获取通讯录的4种方式

    在iOS开发中,获取用户设备上的通讯录信息是一项常见的需求,比如为了推荐好友或同步联系人数据。本文将详细介绍四种不同的方法来实现这一功能,针对不同的iOS版本和使用场景。 ### 方案一:AddressBookUI....

    iphone contact demo

    在iOS开发中,获取iPhone联系人(Contacts)是常见的需求,比如创建社交应用或者需要同步用户通讯录信息的场景。本示例项目“iPhone Contact Demo”提供了一个基础的实现方式,帮助开发者理解如何使用AddressBook...

    iOS开发中使app获取本机通讯录的实现代码实例

    4. **处理联系人属性** 上述代码中,我们仅获取了联系人的名字(ABPersonCompositeNameFormatFirstNameFirst),但实际应用中可能还需要获取其他属性,如电话号码(kABPersonPhoneProperty)、电子邮件...

    IOS 开发之读取addressbook的实现实例

    接下来,获取AddressBook中所有的联系人记录: ```objc CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook); ``` `people`是一个`CFArrayRef`类型的数组,包含了所有联系人的记录。你可以遍历这...

Global site tag (gtag.js) - Google Analytics