iOS 通訊錄的操作

  1. 爲了調用系統的通訊錄界面與相應功能,需要引入AddressBook.framework  
  2.   
  3. 同時,在源文件中需要包含同文件:  
  4. [html] view plaincopy  
  5.   
  6.     #import <AddressBook/AddressBook.h>    
  7.     #import <AddressBookUI/AddressBookUI.h>    
  8.   
  9. 讀取手機通訊錄  
  10. ABAddressBookRef addressBook = ABAddressBookCreate();  
  11.   
  12. 讀取聯繫人 小明  
  13. CFStringRef cfName = CFSTR("小明");  
  14. NSArray *people = (NSArray*)ABAddressBookCopyPeopleWithName(myAddressBook, cfName);  
  15.   
  16. people就是名字爲小明的聯繫人數組。默認對象是CFArray,取長度方法爲:CFArrayGetCount(people)  
  17. 爲了方便強制轉換成了NSArray  
  18.   
  19. 其中一個小明  
  20. [html] view plaincopy  
  21.   
  22.     if(people != nil && [people count]>0){    
  23.             ABRecordRef aXiaoming0 = CFArrayGetValueAtIndex(people,0);    
  24.     }    
  25.         
  26.     //獲取小明0的名字    
  27.     CFStringRef cfname = ABRecordCopyValue(aXiaoming0, kABPersonFirstNameProperty);    
  28.         
  29.     //獲取小明0的電話信息    
  30.     ABMultiValueRef cfphone = ABRecordCopyValue(aXiaoming0, kABPersonPhoneProperty);    
  31.         
  32.     //獲取小明0的第0個電話類型:(比如 工作,住宅,iphone,移動電話等)    
  33.     CFStringRef leixin = ABMultiValueCopyLabelAtIndex(cfphone,0);    
  34.         
  35.     //獲取小明0的第3個電話號碼:(使用前先判斷長度ABMultiValueGetCount(cfphone)>4)    
  36.     CFStringRef haoma = ABMultiValueCopyValueAtIndex(cfphone,3);    
  37.         
  38.     //添加一個聯繫人    
  39.         
  40.     CFErrorRef anError = NULL;    
  41.     ABRecordRef aContact = ABPersonCreate();//聯繫人    
  42.         
  43.     //名字    
  44.     NSString* name = @"小利";    
  45.     CFStringRef cfsname = CFStringCreateWithCString( kCFAllocatorDefault, [name UTF8String], kCFStringEncodingUTF8);    
  46.     ABRecordSetValue(aContact, kABPersonFirstNameProperty, cfsname, &anError);//寫入名字進聯繫人    
  47.         
  48.     //號碼    
  49.     ABMultiValueRef phone =ABMultiValueCreateMutable(kABMultiStringPropertyType);    
  50.     ABMultiValueAddValueAndLabel(phone, @“13800138000”,kABPersonPhoneMobileLabel, NULL);//添加移動號碼0    
  51.     ABMultiValueAddValueAndLabel(phone, @“18688888888”,kABPersonPhoneIPhoneLabel, NULL);//添加iphone號碼1    
  52.     //⋯⋯ 添加多個號碼    
  53.         
  54.     ABRecordSetValue(aContact, kABPersonPhoneProperty, phone, &anError);//寫入全部號碼進聯繫人    
  55.         
  56.     ABAddressBookAddRecord(addressBook, aContact, &anError);//寫入通訊錄    
  57.     ABAddressBookSave(addressBook, &error);//保存    
  58.     //注意釋放各數據    
  59.     CFRelease(cfsname);    
  60.     CFRelease(phone);    
  61.     CFRelease(aContact);    
  62.     CFRelease(addressBook);    
  63.   
  64.   
  65. 獲取所有聯繫人  
  66.   
  67. [html] view plaincopy  
  68.   
  69.     CFArrayRef allperson =ABAddressBookCopyArrayOfAllPeople(addressBook);    
  70.     for (id person in (NSArray *)allperson) {    
  71.     }    
  72.   
  73. 添加聯繫人  
  74. [html] view plaincopy  
  75.   
  76.     //name    
  77.      ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();    
  78.      ABRecordRef newPerson = ABPersonCreate();    
  79.      CFErrorRef error = NULL;    
  80.      ABRecordSetValue(newPerson, kABPersonFirstNameProperty, firsrName.text, &error);    
  81.      ABRecordSetValue(newPerson, kABPersonLastNameProperty, lastName.text, &error);    
  82.      ABRecordSetValue(newPerson, kABPersonOrganizationProperty, company.text, &error);    
  83.      ABRecordSetValue(newPerson, kABPersonFirstNamePhoneticProperty, firsrNamePY.text, &error);    
  84.      ABRecordSetValue(newPerson, kABPersonLastNamePhoneticProperty, lastNamePY.text, &error);    
  85.      //phone number    
  86.      ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);    
  87.      ABMultiValueAddValueAndLabel(multiPhone, houseNumber.text, kABPersonPhoneHomeFAXLabel, NULL);    
  88.      ABMultiValueAddValueAndLabel(multiPhone, mobileNumber.text, kABPersonPhoneMobileLabel, NULL);    
  89.      ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone, &error);    
  90.      CFRelease(multiPhone);    
  91.      //email    
  92.      ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);    
  93.      ABMultiValueAddValueAndLabel(multiEmail, email.text, kABWorkLabel, NULL);    
  94.      ABRecordSetValue(newPerson, kABPersonEmailProperty, multiEmail, &error);    
  95.      CFRelease(multiEmail);    
  96.      //picture    
  97.      NSData *dataRef = UIImagePNGRepresentation(head.image);    
  98.      ABPersonSetImageData(newPerson, (CFDataRef)dataRef, &error);    
  99.      ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);    
  100.      ABAddressBookSave(iPhoneAddressBook, &error);    
  101.      CFRelease(newPerson);    
  102.      CFRelease(iPhoneAddressBook);    
  103.   
  104.   
  105. 刪除聯繫人  
  106. [html] view plaincopy  
  107.   
  108.     CFErrorRef error = NULL;    
  109.     ABRecordRef oldPeople = ABAddressBookGetPersonWithRecordID(iPhoneAddressBook, recordID);    
  110.     if (!oldPeople) {    
  111.         return;    
  112.     }    
  113.     ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();    
  114.     ABAddressBookRemoveRecord(iPhoneAddressBook, oldPeople, &error);    
  115.     ABAddressBookSave(iPhoneAddressBook, &error);    
  116.     CFRelease(iPhoneAddressBook);    
  117.     CFRelease(oldPeople);    
  118.   
  119.   
  120. 獲取所有組  
  121.   
  122.   
  123. [html] view plaincopy  
  124.   
  125.     CFArrayRef array = ABAddressBookCopyArrayOfAllGroups(iPhoneAddressBook);    
  126.     for (id group in (NSArray *)array) {    
  127.         NSLog(@"group name = %@", ABRecordCopyValue(group, kABGroupNameProperty));    
  128.         NSLog(@"group id = %d", ABRecordGetRecordID(group));    
  129.     }    
  130.   
  131. 刪除組  
  132. [html] view plaincopy  
  133.   
  134.     ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();    
  135.     ABRecordRef oldGroup = ABAddressBookGetGroupWithRecordID(iPhoneAddressBook, RecordID);    
  136.     ABAddressBookRemoveRecord(iPhoneAddressBook, oldGroup, nil);    
  137.     ABAddressBookSave(iPhoneAddressBook, nil);    
  138.     CFRelease(iPhoneAddressBook);    
  139.     CFRelease(oldGroup);    
  140.   
  141.   
  142.   
  143. 添加組  
  144. [html] view plaincopy  
  145.   
  146.     ABAddressBookRef  iPhoneAddressBook = ABAddressBookCreate();    
  147.     ABRecordRef  newGroup = ABGroupCreate();    
  148.     ABRecordSetValue(newGroup, kABGroupNameProperty, groupName.text, nil);    
  149.     ABAddressBookAddRecord(iPhoneAddressBook, newGroup, nil);    
  150.     ABAddressBookSave(iPhoneAddressBook, nil);    
  151.     CFRelease(newGroup);    
  152.     CFRelease(iPhoneAddressBook);    
  153.   
  154.   
  155. 獲得通訊錄中聯繫人的所有屬性  
  156. [html] view plaincopy  
  157.   
  158.     ABAddressBookRef addressBook = ABAddressBookCreate();    
  159.      CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook);    
  160.      for(int i = 0; i < CFArrayGetCount(results); i++)    
  161.      {    
  162.          ABRecordRef person = CFArrayGetValueAtIndex(results, i);    
  163.          //讀取firstname    
  164.          NSString *personName = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);    
  165.          if(personName != nil)    
  166.              textView.text = [textView.text stringByAppendingFormat:@"\n姓名:%@\n",personName];    
  167.          //讀取lastname    
  168.          NSString *lastname = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);    
  169.          if(lastname != nil)    
  170.              textView.text = [textView.text stringByAppendingFormat:@"%@\n",lastname];    
  171.          //讀取middlename    
  172.          NSString *middlename = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);    
  173.          if(middlename != nil)    
  174.              textView.text = [textView.text stringByAppendingFormat:@"%@\n",middlename];    
  175.          //讀取prefix前綴    
  176.          NSString *prefix = (NSString*)ABRecordCopyValue(person, kABPersonPrefixProperty);    
  177.          if(prefix != nil)    
  178.              textView.text = [textView.text stringByAppendingFormat:@"%@\n",prefix];    
  179.          //讀取suffix後綴    
  180.          NSString *suffix = (NSString*)ABRecordCopyValue(person, kABPersonSuffixProperty);    
  181.          if(suffix != nil)    
  182.              textView.text = [textView.text stringByAppendingFormat:@"%@\n",suffix];    
  183.          //讀取nickname呢稱    
  184.          NSString *nickname = (NSString*)ABRecordCopyValue(person, kABPersonNicknameProperty);    
  185.          if(nickname != nil)    
  186.              textView.text = [textView.text stringByAppendingFormat:@"%@\n",nickname];    
  187.          //讀取firstname拼音音標    
  188.          NSString *firstnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty);    
  189.          if(firstnamePhonetic != nil)    
  190.              textView.text = [textView.text stringByAppendingFormat:@"%@\n",firstnamePhonetic];    
  191.          //讀取lastname拼音音標    
  192.          NSString *lastnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty);    
  193.          if(lastnamePhonetic != nil)    
  194.              textView.text = [textView.text stringByAppendingFormat:@"%@\n",lastnamePhonetic];    
  195.          //讀取middlename拼音音標    
  196.          NSString *middlenamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty);    
  197.          if(middlenamePhonetic != nil)    
  198.              textView.text = [textView.text stringByAppendingFormat:@"%@\n",middlenamePhonetic];    
  199.          //讀取organization公司    
  200.          NSString *organization = (NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);    
  201.          if(organization != nil)    
  202.              textView.text = [textView.text stringByAppendingFormat:@"%@\n",organization];    
  203.          //讀取jobtitle工作    
  204.          NSString *jobtitle = (NSString*)ABRecordCopyValue(person, kABPersonJobTitleProperty);    
  205.          if(jobtitle != nil)    
  206.              textView.text = [textView.text stringByAppendingFormat:@"%@\n",jobtitle];    
  207.          //讀取department部門    
  208.          NSString *department = (NSString*)ABRecordCopyValue(person, kABPersonDepartmentProperty);    
  209.          if(department != nil)    
  210.              textView.text = [textView.text stringByAppendingFormat:@"%@\n",department];    
  211.          //讀取birthday生日    
  212.          NSDate *birthday = (NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty);    
  213.          if(birthday != nil)    
  214.              textView.text = [textView.text stringByAppendingFormat:@"%@\n",birthday];    
  215.          //讀取note備忘錄    
  216.          NSString *note = (NSString*)ABRecordCopyValue(person, kABPersonNoteProperty);    
  217.          if(note != nil)    
  218.              textView.text = [textView.text stringByAppendingFormat:@"%@\n",note];    
  219.          //第一次添加該條記錄的時間    
  220.          NSString *firstknow = (NSString*)ABRecordCopyValue(person, kABPersonCreationDateProperty);    
  221.          NSLog(@"第一次添加該條記錄的時間%@\n",firstknow);    
  222.          //最後一次修改該條記錄的時間    
  223.          NSString *lastknow = (NSString*)ABRecordCopyValue(person, kABPersonModificationDateProperty);    
  224.          NSLog(@"最後一次修改該條記錄的時間%@\n",lastknow);    
  225.              
  226.          //獲取email多值    
  227.          ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);    
  228.          int emailcount = ABMultiValueGetCount(email);    
  229.          for (int x = 0; x < emailcount; x++)    
  230.          {    
  231.              //獲取email Label    
  232.              NSString* emailLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(email, x));    
  233.              //獲取email值    
  234.              NSString* emailContent = (NSString*)ABMultiValueCopyValueAtIndex(email, x);    
  235.              textView.text = [textView.text stringByAppendingFormat:@"%@:%@\n",emailLabel,emailContent];    
  236.          }    
  237.          //讀取地址多值    
  238.          ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);    
  239.          int count = ABMultiValueGetCount(address);    
  240.              
  241.          for(int j = 0; j < count; j++)    
  242.          {    
  243.              //獲取地址Label    
  244.              NSString* addressLabel = (NSString*)ABMultiValueCopyLabelAtIndex(address, j);    
  245.              textView.text = [textView.text stringByAppendingFormat:@"%@\n",addressLabel];    
  246.              //獲取該label下的地址6屬性    
  247.              NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(address, j);    
  248.              NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];    
  249.              if(country != nil)    
  250.                  textView.text = [textView.text stringByAppendingFormat:@"國家:%@\n",country];    
  251.              NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];    
  252.              if(city != nil)    
  253.                  textView.text = [textView.text stringByAppendingFormat:@"城市:%@\n",city];    
  254.              NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];    
  255.              if(state != nil)    
  256.                  textView.text = [textView.text stringByAppendingFormat:@"省:%@\n",state];    
  257.              NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];    
  258.              if(street != nil)    
  259.                  textView.text = [textView.text stringByAppendingFormat:@"街道:%@\n",street];    
  260.              NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey];    
  261.              if(zip != nil)    
  262.                  textView.text = [textView.text stringByAppendingFormat:@"郵編:%@\n",zip];    
  263.              NSString* coutntrycode = [personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey];    
  264.              if(coutntrycode != nil)    
  265.                  textView.text = [textView.text stringByAppendingFormat:@"國家編號:%@\n",coutntrycode];    
  266.          }    
  267.              
  268.          //獲取dates多值    
  269.          ABMultiValueRef dates = ABRecordCopyValue(person, kABPersonDateProperty);    
  270.          int datescount = ABMultiValueGetCount(dates);    
  271.          for (int y = 0; y < datescount; y++)    
  272.          {    
  273.              //獲取dates Label    
  274.              NSString* datesLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(dates, y));    
  275.              //獲取dates值    
  276.              NSString* datesContent = (NSString*)ABMultiValueCopyValueAtIndex(dates, y);    
  277.              textView.text = [textView.text stringByAppendingFormat:@"%@:%@\n",datesLabel,datesContent];    
  278.          }    
  279.          //獲取kind值    
  280.          CFNumberRef recordType = ABRecordCopyValue(person, kABPersonKindProperty);    
  281.          if (recordType == kABPersonKindOrganization) {    
  282.              // it's a company    
  283.              NSLog(@"it's a company\n");    
  284.          } else {    
  285.              // it's a person, resource, or room    
  286.              NSLog(@"it's a person, resource, or room\n");    
  287.          }    
  288.              
  289.              
  290.          //獲取IM多值    
  291.          ABMultiValueRef instantMessage = ABRecordCopyValue(person, kABPersonInstantMessageProperty);    
  292.          for (int l = 1; l < ABMultiValueGetCount(instantMessage); l++)    
  293.          {    
  294.              //獲取IM Label    
  295.              NSString* instantMessageLabel = (NSString*)ABMultiValueCopyLabelAtIndex(instantMessage, l);    
  296.              textView.text = [textView.text stringByAppendingFormat:@"%@\n",instantMessageLabel];    
  297.              //獲取該label下的2屬性    
  298.              NSDictionary* instantMessageContent =(NSDictionary*) ABMultiValueCopyValueAtIndex(instantMessage, l);    
  299.              NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey];    
  300.              if(username != nil)    
  301.                  textView.text = [textView.text stringByAppendingFormat:@"username:%@\n",username];    
  302.                  
  303.              NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey];    
  304.              if(service != nil)    
  305.                  textView.text = [textView.text stringByAppendingFormat:@"service:%@\n",service];    
  306.          }    
  307.              
  308.          //讀取電話多值    
  309.          ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);    
  310.          for (int k = 0; k<ABMultiValueGetCount(phone); k++)    
  311.          {    
  312.              //獲取電話Label    
  313.              NSString * personPhoneLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));    
  314.              //獲取該Label下的電話值    
  315.              NSString * personPhone = (NSString*)ABMultiValueCopyValueAtIndex(phone, k);    
  316.                  
  317.              textView.text = [textView.text stringByAppendingFormat:@"%@:%@\n",personPhoneLabel,personPhone];    
  318.          }    
  319.              
  320.          //獲取URL多值    
  321.          ABMultiValueRef url = ABRecordCopyValue(person, kABPersonURLProperty);    
  322.          for (int m = 0; m < ABMultiValueGetCount(url); m++)    
  323.          {    
  324.              //獲取電話Label    
  325.              NSString * urlLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(url, m));    
  326.              //獲取該Label下的電話值    
  327.              NSString * urlContent = (NSString*)ABMultiValueCopyValueAtIndex(url,m);    
  328.                  
  329.              textView.text = [textView.text stringByAppendingFormat:@"%@:%@\n",urlLabel,urlContent];    
  330.          }    
  331.              
  332.          //讀取照片    
  333.          NSData *image = (NSData*)ABPersonCopyImageData(person);    
  334.              
  335.              
  336.          UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(20005050)];    
  337.          [myImage setImage:[UIImage imageWithData:image]];    
  338.          myImage.opaque = YES;    
  339.          [textView addSubview:myImage];    
  340.              
  341.              
  342.              
  343.      }    
  344.          
  345.      CFRelease(results);    
  346.      CFRelease(addressBook);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章