os提供了對通訊錄操作的組建,其中一個是直接操作通訊錄,另一個是調用通訊錄的UI組建。實現方法如下:
添加AddressBook.framework到工程中。
代碼實現:
-(IBAction)onClickbutton:(id)sender
{
NSMutableArray* personArray =[[[NSMutableArray alloc] init] autorelease];
ABAddressBookRef addressBook =ABAddressBookCreate();
NSString*firstName,*lastName,*fullName;
personArray =(NSMutableArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
if([sender tag]==0){
for(id *person in personArray)
{
firstName =(NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
firstName =[firstName stringByAppendingFormat:@" "];
lastName =(NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
fullName =[firstName stringByAppendingFormat:@"%@",lastName];
NSLog(@"===%@",fullName);
ABMultiValueRef phones =(ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
for(int i =0;i <ABMultiValueGetCount(phones); i++)
{
NSString*phone =(NSString*)ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"===%@",phone);
}
ABMultiValueRef mails =(ABMultiValueRef)ABRecordCopyValue(person, kABPersonEmailProperty);
for(int i =0;i <ABMultiValueGetCount(mails); i++)
{
NSString*mail =(NSString*)ABMultiValueCopyValueAtIndex(mails, i);
NSLog(@"==%@",mail);
}
}
}else{
//刪除資訊
//返回所有連絡人到一個數組中
CFArrayRef personArray =ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex personCount =ABAddressBookGetPersonCount(addressBook);
for(int i =0;i<personCount;i++){
ABRecordRefref=CFArrayGetValueAtIndex(personArray, i);
CFStringRef firstName1 =ABRecordCopyValue(ref, kABPersonFirstNameProperty);
CFStringRef lastName1 =ABRecordCopyValue(ref, kABPersonLastNameProperty);
NSString*contactFirstLast =[NSString stringWithFormat: @"%@%@",(NSString*)firstName1,(NSString*)lastName1];
if([contactFirstLast isEqualToString:@"徐夢"]){
//刪除連絡人
ABAddressBookRemoveRecord(addressBook,ref,nil);
}
}
//儲存電話本
ABAddressBookSave(addressBook,nil);
//釋放記憶體
//CFRelease(personRef);
// CFRelease(addressbookRef);
}
}風之境地 java-javascript 蘑菇街女裝