ios操作通讯录
為了調(diào)用系統(tǒng)的通訊錄界面與相應(yīng)功能,需要引入AddressBook.framework
同時(shí),在源文件中需要包含同文件:
[html] view plaincopy
? ? #import <AddressBook/AddressBook.h> ?
? ? #import <AddressBookUI/AddressBookUI.h> ?
讀取手機(jī)通訊錄
ABAddressBookRef addressBook = ABAddressBookCreate();
讀取聯(lián)系人 小明
CFStringRef cfName = CFSTR("小明");
NSArray *people = (NSArray*)ABAddressBookCopyPeopleWithName(myAddressBook, cfName);
people就是名字為小明的聯(lián)系人數(shù)組。默認(rèn)對(duì)象是CFArray,取長度方法為:CFArrayGetCount(people)
為了方便強(qiáng)制轉(zhuǎn)換成了NSArray
其中一個(gè)小明
[html] view plaincopy
? ? if(people != nil && [people count]>0){ ?
? ? ? ? ? ? ABRecordRef aXiaoming0 = CFArrayGetValueAtIndex(people,0); ?
? ? } ?
? ? ??
? ? //獲取小明0的名字 ?
? ? CFStringRef cfname = ABRecordCopyValue(aXiaoming0, kABPersonFirstNameProperty); ?
? ? ??
? ? //獲取小明0的電話信息 ?
? ? ABMultiValueRef cfphone = ABRecordCopyValue(aXiaoming0, kABPersonPhoneProperty); ?
? ? ??
? ? //獲取小明0的第0個(gè)電話類型:(比如 工作,住宅,iphone,移動(dòng)電話等) ?
? ? CFStringRef leixin = ABMultiValueCopyLabelAtIndex(cfphone,0); ?
? ? ??
? ? //獲取小明0的第3個(gè)電話號(hào)碼:(使用前先判斷長度ABMultiValueGetCount(cfphone)>4) ?
? ? CFStringRef haoma = ABMultiValueCopyValueAtIndex(cfphone,3); ?
? ? ??
? ? //添加一個(gè)聯(lián)系人 ?
? ? ??
? ? CFErrorRef anError = NULL; ?
? ? ABRecordRef aContact = ABPersonCreate();//聯(lián)系人 ?
? ? ??
? ? //名字 ?
? ? NSString* name = @"小利"; ?
? ? CFStringRef cfsname = CFStringCreateWithCString( kCFAllocatorDefault, [name UTF8String], kCFStringEncodingUTF8); ?
? ? ABRecordSetValue(aContact, kABPersonFirstNameProperty, cfsname, &anError);//寫入名字進(jìn)聯(lián)系人 ?
? ? ??
? ? //號(hào)碼 ?
? ? ABMultiValueRef phone =ABMultiValueCreateMutable(kABMultiStringPropertyType); ?
? ? ABMultiValueAddValueAndLabel(phone, @“13800138000”,kABPersonPhoneMobileLabel, NULL);//添加移動(dòng)號(hào)碼0 ?
? ? ABMultiValueAddValueAndLabel(phone, @“18688888888”,kABPersonPhoneIPhoneLabel, NULL);//添加iphone號(hào)碼1 ?
? ? //?? 添加多個(gè)號(hào)碼 ?
? ? ??
? ? ABRecordSetValue(aContact, kABPersonPhoneProperty, phone, &anError);//寫入全部號(hào)碼進(jìn)聯(lián)系人 ?
? ? ??
? ? ABAddressBookAddRecord(addressBook, aContact, &anError);//寫入通訊錄 ?
? ? ABAddressBookSave(addressBook, &error);//保存 ?
? ? //注意釋放各數(shù)據(jù) ?
? ? CFRelease(cfsname); ?
? ? CFRelease(phone); ?
? ? CFRelease(aContact); ?
? ? CFRelease(addressBook); ?
獲取所有聯(lián)系人
[html] view plaincopy
? ? CFArrayRef allperson =ABAddressBookCopyArrayOfAllPeople(addressBook); ?
? ? for (id person in (NSArray *)allperson) { ?
? ? } ?
添加聯(lián)系人
[html] view plaincopy
? ? //name ?
? ? ?ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate(); ?
? ? ?ABRecordRef newPerson = ABPersonCreate(); ?
? ? ?CFErrorRef error = NULL; ?
? ? ?ABRecordSetValue(newPerson, kABPersonFirstNameProperty, firsrName.text, &error); ?
? ? ?ABRecordSetValue(newPerson, kABPersonLastNameProperty, lastName.text, &error); ?
? ? ?ABRecordSetValue(newPerson, kABPersonOrganizationProperty, company.text, &error); ?
? ? ?ABRecordSetValue(newPerson, kABPersonFirstNamePhoneticProperty, firsrNamePY.text, &error); ?
? ? ?ABRecordSetValue(newPerson, kABPersonLastNamePhoneticProperty, lastNamePY.text, &error); ?
? ? ?//phone number ?
? ? ?ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType); ?
? ? ?ABMultiValueAddValueAndLabel(multiPhone, houseNumber.text, kABPersonPhoneHomeFAXLabel, NULL); ?
? ? ?ABMultiValueAddValueAndLabel(multiPhone, mobileNumber.text, kABPersonPhoneMobileLabel, NULL); ?
? ? ?ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone, &error); ?
? ? ?CFRelease(multiPhone); ?
? ? ?//email ?
? ? ?ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType); ?
? ? ?ABMultiValueAddValueAndLabel(multiEmail, email.text, kABWorkLabel, NULL); ?
? ? ?ABRecordSetValue(newPerson, kABPersonEmailProperty, multiEmail, &error); ?
? ? ?CFRelease(multiEmail); ?
? ? ?//picture ?
? ? ?NSData *dataRef = UIImagePNGRepresentation(head.image); ?
? ? ?ABPersonSetImageData(newPerson, (CFDataRef)dataRef, &error); ?
? ? ?ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error); ?
? ? ?ABAddressBookSave(iPhoneAddressBook, &error); ?
? ? ?CFRelease(newPerson); ?
? ? ?CFRelease(iPhoneAddressBook); ?
刪除聯(lián)系人
[html] view plaincopy
? ? CFErrorRef error = NULL; ?
? ? ABRecordRef oldPeople = ABAddressBookGetPersonWithRecordID(iPhoneAddressBook, recordID); ?
? ? if (!oldPeople) { ?
? ? ? ? return; ?
? ? } ?
? ? ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate(); ?
? ? ABAddressBookRemoveRecord(iPhoneAddressBook, oldPeople, &error); ?
? ? ABAddressBookSave(iPhoneAddressBook, &error); ?
? ? CFRelease(iPhoneAddressBook); ?
? ? CFRelease(oldPeople); ?
獲取所有組
[html] view plaincopy
? ? CFArrayRef array = ABAddressBookCopyArrayOfAllGroups(iPhoneAddressBook); ?
? ? for (id group in (NSArray *)array) { ?
? ? ? ? NSLog(@"group name = %@", ABRecordCopyValue(group, kABGroupNameProperty)); ?
? ? ? ? NSLog(@"group id = %d", ABRecordGetRecordID(group)); ?
? ? } ?
刪除組
[html] view plaincopy
? ? ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate(); ?
? ? ABRecordRef oldGroup = ABAddressBookGetGroupWithRecordID(iPhoneAddressBook, RecordID); ?
? ? ABAddressBookRemoveRecord(iPhoneAddressBook, oldGroup, nil); ?
? ? ABAddressBookSave(iPhoneAddressBook, nil); ?
? ? CFRelease(iPhoneAddressBook); ?
? ? CFRelease(oldGroup); ?
添加組
[html] view plaincopy
? ? ABAddressBookRef ?iPhoneAddressBook = ABAddressBookCreate(); ?
? ? ABRecordRef ?newGroup = ABGroupCreate(); ?
? ? ABRecordSetValue(newGroup, kABGroupNameProperty, groupName.text, nil); ?
? ? ABAddressBookAddRecord(iPhoneAddressBook, newGroup, nil); ?
? ? ABAddressBookSave(iPhoneAddressBook, nil); ?
? ? CFRelease(newGroup); ?
? ? CFRelease(iPhoneAddressBook); ?
獲得通訊錄中聯(lián)系人的所有屬性
[html] view plaincopy
? ? 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) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"\n姓名:%@\n",personName]; ?
? ? ? ? ?//讀取lastname ?
? ? ? ? ?NSString *lastname = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty); ?
? ? ? ? ?if(lastname != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",lastname]; ?
? ? ? ? ?//讀取middlename ?
? ? ? ? ?NSString *middlename = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty); ?
? ? ? ? ?if(middlename != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",middlename]; ?
? ? ? ? ?//讀取prefix前綴 ?
? ? ? ? ?NSString *prefix = (NSString*)ABRecordCopyValue(person, kABPersonPrefixProperty); ?
? ? ? ? ?if(prefix != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",prefix]; ?
? ? ? ? ?//讀取suffix后綴 ?
? ? ? ? ?NSString *suffix = (NSString*)ABRecordCopyValue(person, kABPersonSuffixProperty); ?
? ? ? ? ?if(suffix != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",suffix]; ?
? ? ? ? ?//讀取nickname呢稱 ?
? ? ? ? ?NSString *nickname = (NSString*)ABRecordCopyValue(person, kABPersonNicknameProperty); ?
? ? ? ? ?if(nickname != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",nickname]; ?
? ? ? ? ?//讀取firstname拼音音標(biāo) ?
? ? ? ? ?NSString *firstnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty); ?
? ? ? ? ?if(firstnamePhonetic != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",firstnamePhonetic]; ?
? ? ? ? ?//讀取lastname拼音音標(biāo) ?
? ? ? ? ?NSString *lastnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty); ?
? ? ? ? ?if(lastnamePhonetic != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",lastnamePhonetic]; ?
? ? ? ? ?//讀取middlename拼音音標(biāo) ?
? ? ? ? ?NSString *middlenamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty); ?
? ? ? ? ?if(middlenamePhonetic != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",middlenamePhonetic]; ?
? ? ? ? ?//讀取organization公司 ?
? ? ? ? ?NSString *organization = (NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty); ?
? ? ? ? ?if(organization != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",organization]; ?
? ? ? ? ?//讀取jobtitle工作 ?
? ? ? ? ?NSString *jobtitle = (NSString*)ABRecordCopyValue(person, kABPersonJobTitleProperty); ?
? ? ? ? ?if(jobtitle != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",jobtitle]; ?
? ? ? ? ?//讀取department部門 ?
? ? ? ? ?NSString *department = (NSString*)ABRecordCopyValue(person, kABPersonDepartmentProperty); ?
? ? ? ? ?if(department != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",department]; ?
? ? ? ? ?//讀取birthday生日 ?
? ? ? ? ?NSDate *birthday = (NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty); ?
? ? ? ? ?if(birthday != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",birthday]; ?
? ? ? ? ?//讀取note備忘錄 ?
? ? ? ? ?NSString *note = (NSString*)ABRecordCopyValue(person, kABPersonNoteProperty); ?
? ? ? ? ?if(note != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",note]; ?
? ? ? ? ?//第一次添加該條記錄的時(shí)間 ?
? ? ? ? ?NSString *firstknow = (NSString*)ABRecordCopyValue(person, kABPersonCreationDateProperty); ?
? ? ? ? ?NSLog(@"第一次添加該條記錄的時(shí)間%@\n",firstknow); ?
? ? ? ? ?//最后一次修改該條記錄的時(shí)間 ?
? ? ? ? ?NSString *lastknow = (NSString*)ABRecordCopyValue(person, kABPersonModificationDateProperty); ?
? ? ? ? ?NSLog(@"最后一次修改該條記錄的時(shí)間%@\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); ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@:%@\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.text stringByAppendingFormat:@"%@\n",addressLabel]; ?
? ? ? ? ? ? ?//獲取該label下的地址6屬性 ?
? ? ? ? ? ? ?NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(address, j); ?
? ? ? ? ? ? ?NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey]; ?
? ? ? ? ? ? ?if(country != nil) ?
? ? ? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"國家:%@\n",country]; ?
? ? ? ? ? ? ?NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey]; ?
? ? ? ? ? ? ?if(city != nil) ?
? ? ? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"城市:%@\n",city]; ?
? ? ? ? ? ? ?NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey]; ?
? ? ? ? ? ? ?if(state != nil) ?
? ? ? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"省:%@\n",state]; ?
? ? ? ? ? ? ?NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey]; ?
? ? ? ? ? ? ?if(street != nil) ?
? ? ? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"街道:%@\n",street]; ?
? ? ? ? ? ? ?NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey]; ?
? ? ? ? ? ? ?if(zip != nil) ?
? ? ? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"郵編:%@\n",zip]; ?
? ? ? ? ? ? ?NSString* coutntrycode = [personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey]; ?
? ? ? ? ? ? ?if(coutntrycode != nil) ?
? ? ? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"國家編號(hào):%@\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); ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@:%@\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); ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",instantMessageLabel]; ?
? ? ? ? ? ? ?//獲取該label下的2屬性 ?
? ? ? ? ? ? ?NSDictionary* instantMessageContent =(NSDictionary*) ABMultiValueCopyValueAtIndex(instantMessage, l); ?
? ? ? ? ? ? ?NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey]; ?
? ? ? ? ? ? ?if(username != nil) ?
? ? ? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"username:%@\n",username]; ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ?NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey]; ?
? ? ? ? ? ? ?if(service != nil) ?
? ? ? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"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); ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@:%@\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.text stringByAppendingFormat:@"%@:%@\n",urlLabel,urlContent]; ?
? ? ? ? ?} ?
? ? ? ? ? ?
? ? ? ? ?//讀取照片 ?
? ? ? ? ?NSData *image = (NSData*)ABPersonCopyImageData(person); ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ?UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(200, 0, 50, 50)]; ?
? ? ? ? ?[myImage setImage:[UIImage imageWithData:image]]; ?
? ? ? ? ?myImage.opaque = YES; ?
? ? ? ? ?[textView addSubview:myImage]; ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ?} ?
? ? ? ?
? ? ?CFRelease(results); ?
? ? ?CFRelease(addressBook);
同時(shí),在源文件中需要包含同文件:
[html] view plaincopy
? ? #import <AddressBook/AddressBook.h> ?
? ? #import <AddressBookUI/AddressBookUI.h> ?
讀取手機(jī)通訊錄
ABAddressBookRef addressBook = ABAddressBookCreate();
讀取聯(lián)系人 小明
CFStringRef cfName = CFSTR("小明");
NSArray *people = (NSArray*)ABAddressBookCopyPeopleWithName(myAddressBook, cfName);
people就是名字為小明的聯(lián)系人數(shù)組。默認(rèn)對(duì)象是CFArray,取長度方法為:CFArrayGetCount(people)
為了方便強(qiáng)制轉(zhuǎn)換成了NSArray
其中一個(gè)小明
[html] view plaincopy
? ? if(people != nil && [people count]>0){ ?
? ? ? ? ? ? ABRecordRef aXiaoming0 = CFArrayGetValueAtIndex(people,0); ?
? ? } ?
? ? ??
? ? //獲取小明0的名字 ?
? ? CFStringRef cfname = ABRecordCopyValue(aXiaoming0, kABPersonFirstNameProperty); ?
? ? ??
? ? //獲取小明0的電話信息 ?
? ? ABMultiValueRef cfphone = ABRecordCopyValue(aXiaoming0, kABPersonPhoneProperty); ?
? ? ??
? ? //獲取小明0的第0個(gè)電話類型:(比如 工作,住宅,iphone,移動(dòng)電話等) ?
? ? CFStringRef leixin = ABMultiValueCopyLabelAtIndex(cfphone,0); ?
? ? ??
? ? //獲取小明0的第3個(gè)電話號(hào)碼:(使用前先判斷長度ABMultiValueGetCount(cfphone)>4) ?
? ? CFStringRef haoma = ABMultiValueCopyValueAtIndex(cfphone,3); ?
? ? ??
? ? //添加一個(gè)聯(lián)系人 ?
? ? ??
? ? CFErrorRef anError = NULL; ?
? ? ABRecordRef aContact = ABPersonCreate();//聯(lián)系人 ?
? ? ??
? ? //名字 ?
? ? NSString* name = @"小利"; ?
? ? CFStringRef cfsname = CFStringCreateWithCString( kCFAllocatorDefault, [name UTF8String], kCFStringEncodingUTF8); ?
? ? ABRecordSetValue(aContact, kABPersonFirstNameProperty, cfsname, &anError);//寫入名字進(jìn)聯(lián)系人 ?
? ? ??
? ? //號(hào)碼 ?
? ? ABMultiValueRef phone =ABMultiValueCreateMutable(kABMultiStringPropertyType); ?
? ? ABMultiValueAddValueAndLabel(phone, @“13800138000”,kABPersonPhoneMobileLabel, NULL);//添加移動(dòng)號(hào)碼0 ?
? ? ABMultiValueAddValueAndLabel(phone, @“18688888888”,kABPersonPhoneIPhoneLabel, NULL);//添加iphone號(hào)碼1 ?
? ? //?? 添加多個(gè)號(hào)碼 ?
? ? ??
? ? ABRecordSetValue(aContact, kABPersonPhoneProperty, phone, &anError);//寫入全部號(hào)碼進(jìn)聯(lián)系人 ?
? ? ??
? ? ABAddressBookAddRecord(addressBook, aContact, &anError);//寫入通訊錄 ?
? ? ABAddressBookSave(addressBook, &error);//保存 ?
? ? //注意釋放各數(shù)據(jù) ?
? ? CFRelease(cfsname); ?
? ? CFRelease(phone); ?
? ? CFRelease(aContact); ?
? ? CFRelease(addressBook); ?
獲取所有聯(lián)系人
[html] view plaincopy
? ? CFArrayRef allperson =ABAddressBookCopyArrayOfAllPeople(addressBook); ?
? ? for (id person in (NSArray *)allperson) { ?
? ? } ?
添加聯(lián)系人
[html] view plaincopy
? ? //name ?
? ? ?ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate(); ?
? ? ?ABRecordRef newPerson = ABPersonCreate(); ?
? ? ?CFErrorRef error = NULL; ?
? ? ?ABRecordSetValue(newPerson, kABPersonFirstNameProperty, firsrName.text, &error); ?
? ? ?ABRecordSetValue(newPerson, kABPersonLastNameProperty, lastName.text, &error); ?
? ? ?ABRecordSetValue(newPerson, kABPersonOrganizationProperty, company.text, &error); ?
? ? ?ABRecordSetValue(newPerson, kABPersonFirstNamePhoneticProperty, firsrNamePY.text, &error); ?
? ? ?ABRecordSetValue(newPerson, kABPersonLastNamePhoneticProperty, lastNamePY.text, &error); ?
? ? ?//phone number ?
? ? ?ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType); ?
? ? ?ABMultiValueAddValueAndLabel(multiPhone, houseNumber.text, kABPersonPhoneHomeFAXLabel, NULL); ?
? ? ?ABMultiValueAddValueAndLabel(multiPhone, mobileNumber.text, kABPersonPhoneMobileLabel, NULL); ?
? ? ?ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone, &error); ?
? ? ?CFRelease(multiPhone); ?
? ? ?//email ?
? ? ?ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType); ?
? ? ?ABMultiValueAddValueAndLabel(multiEmail, email.text, kABWorkLabel, NULL); ?
? ? ?ABRecordSetValue(newPerson, kABPersonEmailProperty, multiEmail, &error); ?
? ? ?CFRelease(multiEmail); ?
? ? ?//picture ?
? ? ?NSData *dataRef = UIImagePNGRepresentation(head.image); ?
? ? ?ABPersonSetImageData(newPerson, (CFDataRef)dataRef, &error); ?
? ? ?ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error); ?
? ? ?ABAddressBookSave(iPhoneAddressBook, &error); ?
? ? ?CFRelease(newPerson); ?
? ? ?CFRelease(iPhoneAddressBook); ?
刪除聯(lián)系人
[html] view plaincopy
? ? CFErrorRef error = NULL; ?
? ? ABRecordRef oldPeople = ABAddressBookGetPersonWithRecordID(iPhoneAddressBook, recordID); ?
? ? if (!oldPeople) { ?
? ? ? ? return; ?
? ? } ?
? ? ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate(); ?
? ? ABAddressBookRemoveRecord(iPhoneAddressBook, oldPeople, &error); ?
? ? ABAddressBookSave(iPhoneAddressBook, &error); ?
? ? CFRelease(iPhoneAddressBook); ?
? ? CFRelease(oldPeople); ?
獲取所有組
[html] view plaincopy
? ? CFArrayRef array = ABAddressBookCopyArrayOfAllGroups(iPhoneAddressBook); ?
? ? for (id group in (NSArray *)array) { ?
? ? ? ? NSLog(@"group name = %@", ABRecordCopyValue(group, kABGroupNameProperty)); ?
? ? ? ? NSLog(@"group id = %d", ABRecordGetRecordID(group)); ?
? ? } ?
刪除組
[html] view plaincopy
? ? ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate(); ?
? ? ABRecordRef oldGroup = ABAddressBookGetGroupWithRecordID(iPhoneAddressBook, RecordID); ?
? ? ABAddressBookRemoveRecord(iPhoneAddressBook, oldGroup, nil); ?
? ? ABAddressBookSave(iPhoneAddressBook, nil); ?
? ? CFRelease(iPhoneAddressBook); ?
? ? CFRelease(oldGroup); ?
添加組
[html] view plaincopy
? ? ABAddressBookRef ?iPhoneAddressBook = ABAddressBookCreate(); ?
? ? ABRecordRef ?newGroup = ABGroupCreate(); ?
? ? ABRecordSetValue(newGroup, kABGroupNameProperty, groupName.text, nil); ?
? ? ABAddressBookAddRecord(iPhoneAddressBook, newGroup, nil); ?
? ? ABAddressBookSave(iPhoneAddressBook, nil); ?
? ? CFRelease(newGroup); ?
? ? CFRelease(iPhoneAddressBook); ?
獲得通訊錄中聯(lián)系人的所有屬性
[html] view plaincopy
? ? 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) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"\n姓名:%@\n",personName]; ?
? ? ? ? ?//讀取lastname ?
? ? ? ? ?NSString *lastname = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty); ?
? ? ? ? ?if(lastname != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",lastname]; ?
? ? ? ? ?//讀取middlename ?
? ? ? ? ?NSString *middlename = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty); ?
? ? ? ? ?if(middlename != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",middlename]; ?
? ? ? ? ?//讀取prefix前綴 ?
? ? ? ? ?NSString *prefix = (NSString*)ABRecordCopyValue(person, kABPersonPrefixProperty); ?
? ? ? ? ?if(prefix != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",prefix]; ?
? ? ? ? ?//讀取suffix后綴 ?
? ? ? ? ?NSString *suffix = (NSString*)ABRecordCopyValue(person, kABPersonSuffixProperty); ?
? ? ? ? ?if(suffix != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",suffix]; ?
? ? ? ? ?//讀取nickname呢稱 ?
? ? ? ? ?NSString *nickname = (NSString*)ABRecordCopyValue(person, kABPersonNicknameProperty); ?
? ? ? ? ?if(nickname != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",nickname]; ?
? ? ? ? ?//讀取firstname拼音音標(biāo) ?
? ? ? ? ?NSString *firstnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty); ?
? ? ? ? ?if(firstnamePhonetic != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",firstnamePhonetic]; ?
? ? ? ? ?//讀取lastname拼音音標(biāo) ?
? ? ? ? ?NSString *lastnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty); ?
? ? ? ? ?if(lastnamePhonetic != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",lastnamePhonetic]; ?
? ? ? ? ?//讀取middlename拼音音標(biāo) ?
? ? ? ? ?NSString *middlenamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty); ?
? ? ? ? ?if(middlenamePhonetic != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",middlenamePhonetic]; ?
? ? ? ? ?//讀取organization公司 ?
? ? ? ? ?NSString *organization = (NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty); ?
? ? ? ? ?if(organization != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",organization]; ?
? ? ? ? ?//讀取jobtitle工作 ?
? ? ? ? ?NSString *jobtitle = (NSString*)ABRecordCopyValue(person, kABPersonJobTitleProperty); ?
? ? ? ? ?if(jobtitle != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",jobtitle]; ?
? ? ? ? ?//讀取department部門 ?
? ? ? ? ?NSString *department = (NSString*)ABRecordCopyValue(person, kABPersonDepartmentProperty); ?
? ? ? ? ?if(department != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",department]; ?
? ? ? ? ?//讀取birthday生日 ?
? ? ? ? ?NSDate *birthday = (NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty); ?
? ? ? ? ?if(birthday != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",birthday]; ?
? ? ? ? ?//讀取note備忘錄 ?
? ? ? ? ?NSString *note = (NSString*)ABRecordCopyValue(person, kABPersonNoteProperty); ?
? ? ? ? ?if(note != nil) ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",note]; ?
? ? ? ? ?//第一次添加該條記錄的時(shí)間 ?
? ? ? ? ?NSString *firstknow = (NSString*)ABRecordCopyValue(person, kABPersonCreationDateProperty); ?
? ? ? ? ?NSLog(@"第一次添加該條記錄的時(shí)間%@\n",firstknow); ?
? ? ? ? ?//最后一次修改該條記錄的時(shí)間 ?
? ? ? ? ?NSString *lastknow = (NSString*)ABRecordCopyValue(person, kABPersonModificationDateProperty); ?
? ? ? ? ?NSLog(@"最后一次修改該條記錄的時(shí)間%@\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); ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@:%@\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.text stringByAppendingFormat:@"%@\n",addressLabel]; ?
? ? ? ? ? ? ?//獲取該label下的地址6屬性 ?
? ? ? ? ? ? ?NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(address, j); ?
? ? ? ? ? ? ?NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey]; ?
? ? ? ? ? ? ?if(country != nil) ?
? ? ? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"國家:%@\n",country]; ?
? ? ? ? ? ? ?NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey]; ?
? ? ? ? ? ? ?if(city != nil) ?
? ? ? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"城市:%@\n",city]; ?
? ? ? ? ? ? ?NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey]; ?
? ? ? ? ? ? ?if(state != nil) ?
? ? ? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"省:%@\n",state]; ?
? ? ? ? ? ? ?NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey]; ?
? ? ? ? ? ? ?if(street != nil) ?
? ? ? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"街道:%@\n",street]; ?
? ? ? ? ? ? ?NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey]; ?
? ? ? ? ? ? ?if(zip != nil) ?
? ? ? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"郵編:%@\n",zip]; ?
? ? ? ? ? ? ?NSString* coutntrycode = [personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey]; ?
? ? ? ? ? ? ?if(coutntrycode != nil) ?
? ? ? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"國家編號(hào):%@\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); ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@:%@\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); ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@\n",instantMessageLabel]; ?
? ? ? ? ? ? ?//獲取該label下的2屬性 ?
? ? ? ? ? ? ?NSDictionary* instantMessageContent =(NSDictionary*) ABMultiValueCopyValueAtIndex(instantMessage, l); ?
? ? ? ? ? ? ?NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey]; ?
? ? ? ? ? ? ?if(username != nil) ?
? ? ? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"username:%@\n",username]; ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ?NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey]; ?
? ? ? ? ? ? ?if(service != nil) ?
? ? ? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"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); ?
? ? ? ? ? ? ? ?
? ? ? ? ? ? ?textView.text = [textView.text stringByAppendingFormat:@"%@:%@\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.text stringByAppendingFormat:@"%@:%@\n",urlLabel,urlContent]; ?
? ? ? ? ?} ?
? ? ? ? ? ?
? ? ? ? ?//讀取照片 ?
? ? ? ? ?NSData *image = (NSData*)ABPersonCopyImageData(person); ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ?UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(200, 0, 50, 50)]; ?
? ? ? ? ?[myImage setImage:[UIImage imageWithData:image]]; ?
? ? ? ? ?myImage.opaque = YES; ?
? ? ? ? ?[textView addSubview:myImage]; ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ?} ?
? ? ? ?
? ? ?CFRelease(results); ?
? ? ?CFRelease(addressBook);
總結(jié)
- 上一篇: html5图片长按保存,一文彻底解决HT
- 下一篇: 2.3线性表的链式存储和运算—双向链表