iOS_获取通讯录数据

来源:互联网 发布:windows下的linux终端 编辑:程序博客网 时间:2024/06/06 01:09
  1. #import <AddressBook/AddressBook.h>  
  2. #import <AddressBookUI/AddressBookUI.h>  
  3.   
  4. -(void)viewDidAppear:(BOOL)animated{  
  5.     ABAddressBookRef addressBook = ABAddressBookCreate();  
  6.       
  7.     CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook);  
  8.       
  9.     for(int i = 0; i < CFArrayGetCount(results); i++)  
  10.     {  
  11.         ABRecordRef person = CFArrayGetValueAtIndex(results, i);  
  12.         //读取firstname  
  13.         NSString *personName = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);  
  14.         if(personName != nil)  
  15.             textView.text = [textView.text stringByAppendingFormat:@"\n姓名:%@\n",personName];  
  16.         //读取lastname  
  17.         NSString *lastname = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);  
  18.         if(lastname != nil)  
  19.             textView.text = [textView.text stringByAppendingFormat:@"%@\n",lastname];  
  20.         //读取middlename  
  21.         NSString *middlename = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);  
  22.         if(middlename != nil)  
  23.             textView.text = [textView.text stringByAppendingFormat:@"%@\n",middlename];  
  24.         //读取prefix前缀  
  25.         NSString *prefix = (NSString*)ABRecordCopyValue(person, kABPersonPrefixProperty);  
  26.         if(prefix != nil)  
  27.             textView.text = [textView.text stringByAppendingFormat:@"%@\n",prefix];  
  28.         //读取suffix后缀  
  29.         NSString *suffix = (NSString*)ABRecordCopyValue(person, kABPersonSuffixProperty);  
  30.         if(suffix != nil)  
  31.             textView.text = [textView.text stringByAppendingFormat:@"%@\n",suffix];  
  32.         //读取nickname呢称  
  33.         NSString *nickname = (NSString*)ABRecordCopyValue(person, kABPersonNicknameProperty);  
  34.         if(nickname != nil)  
  35.             textView.text = [textView.text stringByAppendingFormat:@"%@\n",nickname];  
  36.         //读取firstname拼音音标  
  37.         NSString *firstnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty);  
  38.         if(firstnamePhonetic != nil)  
  39.             textView.text = [textView.text stringByAppendingFormat:@"%@\n",firstnamePhonetic];  
  40.         //读取lastname拼音音标  
  41.         NSString *lastnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty);  
  42.         if(lastnamePhonetic != nil)  
  43.             textView.text = [textView.text stringByAppendingFormat:@"%@\n",lastnamePhonetic];  
  44.         //读取middlename拼音音标  
  45.         NSString *middlenamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty);  
  46.         if(middlenamePhonetic != nil)  
  47.             textView.text = [textView.text stringByAppendingFormat:@"%@\n",middlenamePhonetic];  
  48.         //读取organization公司  
  49.         NSString *organization = (NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);  
  50.         if(organization != nil)  
  51.             textView.text = [textView.text stringByAppendingFormat:@"%@\n",organization];  
  52.         //读取jobtitle工作  
  53.         NSString *jobtitle = (NSString*)ABRecordCopyValue(person, kABPersonJobTitleProperty);  
  54.         if(jobtitle != nil)  
  55.             textView.text = [textView.text stringByAppendingFormat:@"%@\n",jobtitle];  
  56.         //读取department部门  
  57.         NSString *department = (NSString*)ABRecordCopyValue(person, kABPersonDepartmentProperty);  
  58.         if(department != nil)  
  59.             textView.text = [textView.text stringByAppendingFormat:@"%@\n",department];  
  60.         //读取birthday生日  
  61.         NSDate *birthday = (NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty);  
  62.         if(birthday != nil)  
  63.             textView.text = [textView.text stringByAppendingFormat:@"%@\n",birthday];  
  64.         //读取note备忘录  
  65.         NSString *note = (NSString*)ABRecordCopyValue(person, kABPersonNoteProperty);  
  66.         if(note != nil)  
  67.             textView.text = [textView.text stringByAppendingFormat:@"%@\n",note];  
  68.         //第一次添加该条记录的时间  
  69.         NSString *firstknow = (NSString*)ABRecordCopyValue(person, kABPersonCreationDateProperty);  
  70.         NSLog(@"第一次添加该条记录的时间%@\n",firstknow);  
  71.         //最后一次修改該条记录的时间  
  72.         NSString *lastknow = (NSString*)ABRecordCopyValue(person, kABPersonModificationDateProperty);  
  73.         NSLog(@"最后一次修改該条记录的时间%@\n",lastknow);  
  74.           
  75.         //获取email多值  
  76.         ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);  
  77.         int emailcount = ABMultiValueGetCount(email);  
  78.         for (int x = 0; x < emailcount; x++)  
  79.         {  
  80.             //获取email Label  
  81.             NSString* emailLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(email, x));  
  82.             //获取email值  
  83.             NSString* emailContent = (NSString*)ABMultiValueCopyValueAtIndex(email, x);  
  84.             textView.text = [textView.text stringByAppendingFormat:@"%@:%@\n",emailLabel,emailContent];  
  85.         }  
  86.         //读取地址多值  
  87.         ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);  
  88.         int count = ABMultiValueGetCount(address);  
  89.           
  90.         for(int j = 0; j < count; j++)  
  91.         {  
  92.             //获取地址Label  
  93.             NSString* addressLabel = (NSString*)ABMultiValueCopyLabelAtIndex(address, j);  
  94.             textView.text = [textView.text stringByAppendingFormat:@"%@\n",addressLabel];  
  95.             //获取該label下的地址6属性  
  96.             NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(address, j);  
  97.             NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];  
  98.             if(country != nil)  
  99.                 textView.text = [textView.text stringByAppendingFormat:@"国家:%@\n",country];  
  100.             NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];  
  101.             if(city != nil)  
  102.                 textView.text = [textView.text stringByAppendingFormat:@"城市:%@\n",city];  
  103.             NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];  
  104.             if(state != nil)  
  105.                 textView.text = [textView.text stringByAppendingFormat:@"省:%@\n",state];  
  106.             NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];  
  107.             if(street != nil)  
  108.                 textView.text = [textView.text stringByAppendingFormat:@"街道:%@\n",street];  
  109.             NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey];  
  110.             if(zip != nil)  
  111.                 textView.text = [textView.text stringByAppendingFormat:@"邮编:%@\n",zip];  
  112.             NSString* coutntrycode = [personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey];  
  113.             if(coutntrycode != nil)  
  114.                 textView.text = [textView.text stringByAppendingFormat:@"国家编号:%@\n",coutntrycode];  
  115.         }  
  116.           
  117.         //获取dates多值  
  118.         ABMultiValueRef dates = ABRecordCopyValue(person, kABPersonDateProperty);  
  119.         int datescount = ABMultiValueGetCount(dates);  
  120.         for (int y = 0; y < datescount; y++)  
  121.         {  
  122.             //获取dates Label  
  123.             NSString* datesLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(dates, y));  
  124.             //获取dates值  
  125.             NSString* datesContent = (NSString*)ABMultiValueCopyValueAtIndex(dates, y);  
  126.             textView.text = [textView.text stringByAppendingFormat:@"%@:%@\n",datesLabel,datesContent];  
  127.         }  
  128.         //获取kind值  
  129.         CFNumberRef recordType = ABRecordCopyValue(person, kABPersonKindProperty);  
  130.         if (recordType == kABPersonKindOrganization) {  
  131.             // it's a company  
  132.             NSLog(@"it's a company\n");  
  133.         } else {  
  134.             // it's a person, resource, or room  
  135.             NSLog(@"it's a person, resource, or room\n");  
  136.         }  
  137.           
  138.           
  139.         //获取IM多值  
  140.         ABMultiValueRef instantMessage = ABRecordCopyValue(person, kABPersonInstantMessageProperty);  
  141.         for (int l = 1; l < ABMultiValueGetCount(instantMessage); l++)  
  142.         {  
  143.             //获取IM Label  
  144.             NSString* instantMessageLabel = (NSString*)ABMultiValueCopyLabelAtIndex(instantMessage, l);  
  145.             textView.text = [textView.text stringByAppendingFormat:@"%@\n",instantMessageLabel];  
  146.             //获取該label下的2属性  
  147.             NSDictionary* instantMessageContent =(NSDictionary*) ABMultiValueCopyValueAtIndex(instantMessage, l);  
  148.             NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey];  
  149.             if(username != nil)  
  150.                 textView.text = [textView.text stringByAppendingFormat:@"username:%@\n",username];  
  151.               
  152.             NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey];  
  153.             if(service != nil)  
  154.                 textView.text = [textView.text stringByAppendingFormat:@"service:%@\n",service];  
  155.         }  
  156.           
  157.         //读取电话多值  
  158.         ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);  
  159.         for (int k = 0; k<ABMultiValueGetCount(phone); k++)  
  160.         {  
  161.             //获取电话Label  
  162.             NSString * personPhoneLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));  
  163.             //获取該Label下的电话值  
  164.             NSString * personPhone = (NSString*)ABMultiValueCopyValueAtIndex(phone, k);  
  165.               
  166.             textView.text = [textView.text stringByAppendingFormat:@"%@:%@\n",personPhoneLabel,personPhone];  
  167.         }  
  168.           
  169.         //获取URL多值  
  170.         ABMultiValueRef url = ABRecordCopyValue(person, kABPersonURLProperty);  
  171.         for (int m = 0; m < ABMultiValueGetCount(url); m++)  
  172.         {  
  173.             //获取电话Label  
  174.             NSString * urlLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(url, m));  
  175.             //获取該Label下的电话值  
  176.             NSString * urlContent = (NSString*)ABMultiValueCopyValueAtIndex(url,m);  
  177.               
  178.             textView.text = [textView.text stringByAppendingFormat:@"%@:%@\n",urlLabel,urlContent];  
  179.         }  
  180.           
  181.         //读取照片  
  182.         NSData *image = (NSData*)ABPersonCopyImageData(person);  
  183.           
  184.           
  185.         UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(20005050)];  
  186.         [myImage setImage:[UIImage imageWithData:image]];  
  187.         myImage.opaque = YES;  
  188.         [textView addSubview:myImage];  
  189.           
  190.           
  191.           
  192.     }  
  193.       
  194.     CFRelease(results);  
  195.     CFRelease(addressBook);  
  196.   

0 0