iOS 获取通讯录联系人信息

添加两个框架

 AddressBookUI 

和AddressBook两个包

添加一个 textView  加关联   取名textView

viewDidload里面直接

运行就可以看到效果


iOS6之后要用户授权  所以

要家这么一段代码

 self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    ABAddressBookRef addressBook = ABAddressBookCreate();

    __block BOOL accessGranted = NO;

    if (ABAddressBookRequestAccessWithCompletion != NULL) {

        

        // we're on iOS 6

        NSLog(@"on iOS 6 or later, trying to grant access permission");

        

        dispatch_semaphore_t sema = dispatch_semaphore_create(0);

        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {

            accessGranted = granted;

            dispatch_semaphore_signal(sema);

        });

        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

        dispatch_release(sema);

    }

    else { // we're on iOS 5 or older

        

        NSLog(@"on iOS 5 or older, it is OK");

        accessGranted = YES;

    }

    

    if (accessGranted) {

        

        NSLog(@"we got the access right");

    }


//以上写在 AppDelagte里面


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)

           self.textView.text = [self.textView.textstringByAppendingFormat:@"\n姓名:%@\n",personName];

        //读取lastname

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

       if(lastname != nil)

             self.textView.text = [self.textView.textstringByAppendingFormat:@"%@\n",lastname];

        //读取middlename

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

       if(middlename != nil)

             self.textView.text = [self.textView.textstringByAppendingFormat:@"%@\n",middlename];

        //读取prefix前缀

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

       if(prefix != nil)

             self.textView.text = [self.textView.textstringByAppendingFormat:@"%@\n",prefix];

        //读取suffix后缀

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

       if(suffix != nil)

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@\n",suffix];

        //读取nickname呢称

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

       if(nickname != nil)

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@\n",nickname];

        //读取firstname拼音音标

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

       if(firstnamePhonetic != nil)

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@\n",firstnamePhonetic];

        //读取lastname拼音音标

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

       if(lastnamePhonetic != nil)

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@\n",lastnamePhonetic];

        //读取middlename拼音音标

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

       if(middlenamePhonetic != nil)

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@\n",middlenamePhonetic];

        //读取organization公司

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

       if(organization != nil)

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@\n",organization];

        //读取jobtitle工作

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

       if(jobtitle != nil)

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@\n",jobtitle];

        //读取department部门

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

       if(department != nil)

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@\n",department];

        //读取birthday生日

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

       if(birthday != nil)

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@\n",birthday];

        //读取note备忘录

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

       if(note != nil)

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@\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);

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@:%@\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);

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@\n",addressLabel];

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

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

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

           if(country != nil)

               self.textView.text = [self.textView.textstringByAppendingFormat:@"国家:%@\n",country];

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

           if(city != nil)

               self.textView.text = [self.textView.textstringByAppendingFormat:@"城市:%@\n",city];

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

           if(state != nil)

               self.textView.text = [self.textView.textstringByAppendingFormat:@"省:%@\n",state];

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

           if(street != nil)

               self.textView.text = [self.textView.textstringByAppendingFormat:@"街道:%@\n",street];

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

           if(zip != nil)

               self.textView.text = [self.textView.textstringByAppendingFormat:@"邮编:%@\n",zip];

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

           if(coutntrycode != nil)

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

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@:%@\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);

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@\n",instantMessageLabel];

            //获取該label下的2属性

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

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

           if(username != nil)

               self.textView.text = [self.textView.textstringByAppendingFormat:@"username%@\n",username];

            

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

           if(service != nil)

               self.textView.text = [self.textView.textstringByAppendingFormat:@"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);

            

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@:%@\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);

            

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@:%@\n",urlLabel,urlContent];

        }

        

       //读取照片

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

        

        

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

        [myImagesetImage:[UIImageimageWithData:image]];

        myImage.opaque =YES;

        [self.textViewaddSubview:myImage];

        

        

        

    }

    

   CFRelease(results);

   CFRelease(addressBook);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值