关闭

iOS通讯录读写

111人阅读 评论(0) 收藏 举报
分类:

首先引入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);
    }




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5442次
    • 积分:164
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:2篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论