ios实现通讯录的查询与删除

ios提供了对通讯录操作的组建,其中一个是直接操作通讯录,另一个是调用通讯录的UI组建。实现方法如下:

添加AddressBook.framework到工程中。

image

代码实现:

 

-(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++){ 
              ABRecordRef ref = 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);  
    } 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值