天天看點

IOS學習之擷取iphone通訊錄

iPhone中聯系人的資訊是存放在系統資料庫中的。資料庫中的每一筆記錄都是一個ABRecordRef執行個體。

  通訊錄主要存放兩種記錄:

  1、聯系人的資訊:是ABPerson類型。主要包括聯系人的姓名,電話号碼,位址資訊。

  2、分組資訊:是ABGroup類型。用于将聯系人分到不同的組中。它隻有一個屬性,表示分組名稱。

  在viewDidLoad中添加下面代碼,擷取所有聯系人和分組資訊:

  ABAddressBookRef addressBook =ABAddressBookCreate();

  CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);

  CFArrayRef allGroups =ABAddressBookCopyArrayOfAllGroups(addressBook);

  for (id person in (NSArray *) allPeople)

  [self logContact:person];

  for (id group in (NSArray *) allGroups)

  [self logGroup:group];

  CFRelease(allGroups);

  CFRelease(allPeople);

  CFRelease(addressBook);

  使用ABAddressBookCreate建立一個AddressBook執行個體。ABAddressBookCopyArrayOfAllPeople和ABAddressBookCopyArrayOfAllGroups查詢出所有的聯系人和分組資訊。通過循環,使用logPerson和logGroup兩個方法将資訊輸出到控制台上。

  logPerson方法:

  CFStringRef name = ABRecordCopyCompositeName(person);ABRecordIDrecId = ABRecordGetRecordID(person);NSLog(@"Person Name: %@RecordID:%d",name, recId);

  logGroup方法:

  CFStringRef name =ABRecordCopyValue(group,kABGroupNameProperty);ABRecordID recId =ABRecordGetRecordID(group);NSLog(@"Group Name: %@RecordID:%d",name, recId);

擷取通訊錄中聯系人所有屬性

導入庫:AddressBook.framework   AddressBookUI.framework

#import<AddressBookUI/AddressBookUI.h>

#import<AddressBook/AddressBook.h>

ABAddressBookRef addressBook =ABAddressBookCreate();

CFArrayRef results =ABAddressBookCopyArrayOfAllPeople(addressBook);

for(int i = 0; i< CFArrayGetCount(results); i++)

{

    ABRecordRefperson = CFArrayGetValueAtIndex(results, i);

   //讀取firstname

    NSString*personName =(NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);

    if(personName!= nil)

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

   //讀取lastname

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

    if(lastname!= nil)

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

   //讀取middlename

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

    if(middlename!= nil)

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

   //讀取prefix字首

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

    if(prefix !=nil)

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

   //讀取suffix字尾

    NSString*suffix =(NSString*)ABRecordCopyValue(person,kABPersonSuffixProperty);//kABPers*****uffixProperty

    if(suffix !=nil)

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

   //讀取nickname呢稱

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

    if(nickname!= nil)

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

   //讀取firstname拼音音标

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

   if(firstnamePhonetic != nil)

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

   //讀取lastname拼音音标

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

   if(lastnamePhonetic != nil)

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

   //讀取middlename拼音音标

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

   if(middlenamePhonetic != nil)

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

   //讀取organization公司

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

   if(organization != nil)

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

   //讀取jobtitle工作

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

    if(jobtitle!= nil)

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

   //讀取department部門

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

    if(department!= nil)

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

   //讀取birthday生日

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

    if(birthday!= nil)

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

   //讀取note備忘錄

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

    if(note !=nil)

       textView.text =[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);

    intemailcount = 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.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);

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

      //擷取該label下的位址6屬性

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

       NSString* country = [personaddressvalueForKey:(NSString*)kABPersonAddressCountryKey];

       if(country != nil)

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

       NSString* city = [personaddressvalueForKey:(NSString*)kABPersonAddressCityKey];

       if(city != nil)

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

       NSString* state = [personaddressvalueForKey:(NSString*)kABPersonAddressStateKey];

       if(state != nil)

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

       NSString* street = [personaddressvalueForKey:(NSString*)kABPersonAddressStreetKey];

       if(street != nil)

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

       NSString* zip = [personaddressvalueForKey:(NSString*)kABPersonAddressZIPKey];

       if(zip != nil)

          textView.text =[textView.textstringByAppendingFormat:@"郵編:%@n",zip];   

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

       if(coutntrycode !=nil)

          textView.text =[textView.textstringByAppendingFormat:@"國家編号:%@n",coutntrycode];   

    }

   //擷取dates多值

   ABMultiValueRef dates =ABRecordCopyValue(person,kABPersonDateProperty);

    intdatescount = 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.textstringByAppendingFormat:@"%@:%@n",datesLabel,datesContent];

    }

   //擷取kind值

    CFNumberRefrecordType =ABRecordCopyValue(person,kABPersonKindProperty);

   if (recordType ==kABPersonKindOrganization) {

       // it's a company

       NSLog(@"it's acompanyn");

    } else {

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

       NSLog(@"it's aperson, resource, or roomn");

    }

   //擷取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.textstringByAppendingFormat:@"%@n",instantMessageLabel];

      //擷取該label下的2屬性

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

       NSString* username =[instantMessageContentvalueForKey:(NSString*)kABPersonInstantMessageUsernameKey];

       if(username !=nil)

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

       NSString* service =[instantMessageContentvalueForKey:(NSString*)kABPersonInstantMessageServiceKey];

       if(service != nil)

          textView.text =[textView.textstringByAppendingFormat:@"service:%@n",service];          

    }

   //讀取電話多值

   ABMultiValueRef phone =ABRecordCopyValue(person,kABPersonPhoneProperty);

    for(int k = 0;k<<spanclass="s5">ABMultiValueGetCount(phone); k++)

    {

       //擷取電話Label

       NSString *personPhoneLabel =(NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone,k));

      //擷取該Label下的電話值

       NSString * personPhone =(NSString*)ABMultiValueCopyValueAtIndex(phone,k);

       textView.text =[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);

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

    }

   //讀取照片

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

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

    [myImagesetImage:[UIImageimageWithData:image]];

   myImage.opaque = YES;

    [textViewaddSubview:myImage];

}

CFRelease(results);

CFRelease(addressBook);