iOS通讯录读写

首先引入addressBook.framework addressBookUI.framework框架

首先申请读写权限 
// Request authorization to Address Book
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
    
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            // First time access has been granted, add the contact
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        // The user has previously given access, add the contact
    }
    else {
        // The user has previously denied access
        // Send an alert telling user to change privacy setting in settings app
    }



添加通讯录

ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
        ABRecordRef newPerson = ABPersonCreate();
        
        CFErrorRef error = NULL;
        
        ABRecordSetValue(newPerson, kABPersonFirstNameProperty, (__bridge CFTypeRef)(p.name), &error);
        
        //phone number
        ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFTypeRef)(p.number), kABPersonPhoneMainLabel, NULL);
        //        ABMultiValueAddValueAndLabel(multiPhone, @"1-123-456-7890", kABPersonPhoneMobileLabel, NULL);
        //        ABMultiValueAddValueAndLabel(multiPhone, @"1-987-654-3210", kABOtherLabel, NULL);
        ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,&error);
        CFRelease(multiPhone);
        
        ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
        ABAddressBookSave(iPhoneAddressBook, &error);
        if (error != NULL)
        {
            NSLog(@"Danger Will Robinson! Danger!%@", error);
        }

删除通讯录


//打开电话本数据库
    ABAddressBookRef addressRef=ABAddressBookCreate();
    
    //返回所有联系人到一个数组中
    CFArrayRef personArray = ABAddressBookCopyArrayOfAllPeople(addressRef);
    
    //返回联系人数量
    CFIndex personCount = ABAddressBookGetPersonCount(addressRef);
    
    //循环读取每个联系人
    for (int i =0;i<personCount;i++){
        
        //得到当前联系人
        
        ABRecordRef personRef=CFArrayGetValueAtIndex(personArray, i);
        
        NSString *firstName = (__bridge NSString *)ABRecordCopyValue(personRef, kABPersonFirstNameProperty);
        
        
        ABMultiValueRef phones = (ABMultiValueRef) ABRecordCopyValue(personRef, kABPersonPhoneProperty);
        
        NSString *phone;
        
        for(int i = 0 ;i < ABMultiValueGetCount(phones); i++)
        {
            phone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phones, i);
        }
        
        
        NSLog(@"%@-%@", firstName, phone);
        
        for (Person *p in self.array) {
            if ([p.name isEqual:firstName] && [p.number isEqual:phone]) {
                //删除联系人
                ABAddressBookRemoveRecord(addressRef, personRef, nil);
                
            }
        }
        CFRelease(personRef);
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值