通讯录联系人各属性获取

来源:互联网 发布:淘宝无线端优惠券链接 编辑:程序博客网 时间:2024/05/16 17:21

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)

            label.text = [label.textstringByAppendingFormat:@"姓名:%@",personName];

        //读取lastname

        NSString *lastname = (NSString*)ABRecordCopyValue(person,kABPersonLastNameProperty);

       if(lastname != nil)

            label.text = [label.textstringByAppendingFormat:@"%@",lastname];

        //读取middlename

        NSString *middlename = (NSString*)ABRecordCopyValue(person,kABPersonMiddleNameProperty);

       if(middlename != nil)

            label.text = [label.textstringByAppendingFormat:@"%@",middlename];

        //读取prefix前缀

        NSString *prefix = (NSString*)ABRecordCopyValue(person,kABPersonPrefixProperty);

       if(prefix != nil)

            label.text = [label.textstringByAppendingFormat:@"%@",prefix];

        //读取suffix后缀

        NSString *suffix = (NSString*)ABRecordCopyValue(person,kABPersonSuffixProperty);

       if(suffix != nil)

            label.text = [label.textstringByAppendingFormat:@"%@",suffix];

        //读取nickname呢称

        NSString *nickname = (NSString*)ABRecordCopyValue(person,kABPersonNicknameProperty);

       if(nickname != nil)

            label.text = [label.textstringByAppendingFormat:@"%@",nickname];

        //读取firstname拼音音标

        NSString *firstnamePhonetic = (NSString*)ABRecordCopyValue(person,kABPersonFirstNamePhoneticProperty);

       if(firstnamePhonetic != nil)

            label.text = [label.textstringByAppendingFormat:@"%@",firstnamePhonetic];

        //读取lastname拼音音标

        NSString *lastnamePhonetic = (NSString*)ABRecordCopyValue(person,kABPersonLastNamePhoneticProperty);

       if(lastnamePhonetic != nil)

            label.text = [label.textstringByAppendingFormat:@"%@",lastnamePhonetic];

        //读取middlename拼音音标

        NSString *middlenamePhonetic = (NSString*)ABRecordCopyValue(person,kABPersonMiddleNamePhoneticProperty);

       if(middlenamePhonetic != nil)

           label.text = [label.textstringByAppendingFormat:@"%@",middlenamePhonetic];

        //读取organization公司

        NSString *organization = (NSString*)ABRecordCopyValue(person,kABPersonOrganizationProperty);

       if(organization != nil)

            label.text = [label.textstringByAppendingFormat:@"%@",organization];

        //读取jobtitle工作

        NSString *jobtitle = (NSString*)ABRecordCopyValue(person,kABPersonJobTitleProperty);

       if(jobtitle != nil)

            label.text = [label.textstringByAppendingFormat:@"%@",jobtitle];

        //读取department部门

        NSString *department = (NSString*)ABRecordCopyValue(person,kABPersonDepartmentProperty);

       if(department != nil)

            label.text = [label.textstringByAppendingFormat:@"%@",department];

        //读取birthday生日

        NSDate *birthday = (NSDate*)ABRecordCopyValue(person,kABPersonBirthdayProperty);

       if(birthday != nil)

            label.text = [label.textstringByAppendingFormat:@"%@",birthday];

        //读取note备忘录

        NSString *note = (NSString*)ABRecordCopyValue(person,kABPersonNoteProperty);

       if(note != nil)

            label.text = [label.textstringByAppendingFormat:@"%@",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);

           label.text = [label.textstringByAppendingFormat:@"%@:%@",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);

            label.text = [label.textstringByAppendingFormat:@"%@",addressLabel];

            //获取該label下的地址6属性

           NSDictionary* personaddress =(NSDictionary*)ABMultiValueCopyValueAtIndex(address, j);

           NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];

           if(country != nil)

                label.text = [label.textstringByAppendingFormat:@"国家:%@",country];

           NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];

           if(city != nil)

                label.text = [label.textstringByAppendingFormat:@"城市:%@",city];

           NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];

           if(state != nil)

                label.text = [label.textstringByAppendingFormat:@"省:%@",state];

           NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];

           if(street != nil)

                label.text = [label.textstringByAppendingFormat:@"街道:%@",street];

           NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey];

           if(zip != nil)

                label.text = [label.textstringByAppendingFormat:@"邮编:%@",zip];

           NSString* coutntrycode = [personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey];

           if(coutntrycode != nil)

                label.text = [label.textstringByAppendingFormat:@"国家编号:%@",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);

           label.text = [label.textstringByAppendingFormat:@"%@:%@",datesLabel,datesContent];

        }

        //获取kind

        CFNumberRef recordType =ABRecordCopyValue(person, kABPersonKindProperty);

       if (recordType == kABPersonKindOrganization) {

            // it's a company

            NSLog(@"it's a company");

        }else {

            // it's a person, resource, or room

            NSLog(@"it's a person, resource, or room");

        }

        

        

       //获取IM多值

        ABMultiValueRef instantMessage =ABRecordCopyValue(person, kABPersonInstantMessageProperty);

       for (int l =1; l < ABMultiValueGetCount(instantMessage); l++)

        {

           //获取IM Label

           NSString* instantMessageLabel = (NSString*)ABMultiValueCopyLabelAtIndex(instantMessage, l);

           label.text = [label.textstringByAppendingFormat:@"%@",instantMessageLabel];

            //获取該label下的2属性

           NSDictionary* instantMessageContent =(NSDictionary*)ABMultiValueCopyValueAtIndex(instantMessage, l);

           NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey];

           if(username != nil)

                label.text = [label.textstringByAppendingFormat:@"username%@",username];

            

           NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey];

           if(service != nil)

                label.text = [label.textstringByAppendingFormat:@"service%@",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);

            

           label.text = [label.textstringByAppendingFormat:@"%@:%@",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);

            

           label.text = [label.textstringByAppendingFormat:@"%@:%@",urlLabel,urlContent];

        }

        

       //读取照片

       NSData *image = (NSData*)ABPersonCopyImageData(person);

        

        

       UIImageView *myImage = [[UIImageViewalloc] initWithFrame:CGRectMake(200,0, 50, 50)];

        [myImagesetImage:[UIImageimageWithData:image]];

        myImage.opaque =YES;

        [labeladdSubview:myImage];

    }

    //CF类型变量不适用ARC,需要释放

   CFRelease(results);

   CFRelease(addressBook);


0 0
原创粉丝点击