#import <AddressBook/AddressBook.h>
#pragma mark 删除一个号码 - (void)deleteLocalMarkSuccess:(void(^)(BOOL success))successBlock{ dispatch_async(dispatch_get_global_queue(0, 0), ^{ ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef records; // 获取通讯录中全部联系人 records = ABAddressBookCopyArrayOfAllPeople(addressBook); // 遍历全部联系人,检查已存号码库信息数量 for (int i=0; i<CFArrayGetCount(records); i++) { ABRecordRef record = CFArrayGetValueAtIndex(records, i); CFTypeRef phones = ABRecordCopyValue(record, kABPersonPhoneProperty); // CFArrayRef values = ABMultiValueCopyArrayOfAllValues(phones); // NSArray *arr =(__bridge NSArray *)(values); // if (!arr.count) { // continue; // } NSString *firstphoneNumber = (CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones,0))); NSString *firstLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones,0)); NSLog(@"firstPhone = %@ firstLabel = %@",firstphoneNumber,firstLabel); CFStringRef firstNameRef = ABRecordCopyValue(record, kABPersonFirstNameProperty); //这里是特殊情况的比较号码并删除,正常是比较姓名即可 if ([firstLabel isEqualToString:@"#0云助手号码库yunzhushou"]) { bool done = ABAddressBookRemoveRecord(addressBook, record, nil); successBlock(done); break; } } ABAddressBookSave(addressBook, nil); CFRelease(addressBook); }); }
#pragma mark 删除一个号码属性 - (void)deletePersonPropertyWithNumber:(NSString *)phoneNumber withBlock:(void(^)(BOOL success))successBlock{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_global_queue(0, 0), ^{ ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef records; // 获取通讯录中全部联系人 records = ABAddressBookCopyArrayOfAllPeople(addressBook); __block bool done = NO; // 遍历全部联系人,检查是否存在指定号码再进行删除 for (int i=0; i<CFArrayGetCount(records); i++) { ABRecordRef record = CFArrayGetValueAtIndex(records, i); CFTypeRef phones = ABRecordCopyValue(record, kABPersonPhoneProperty); NSString *firstLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones,0)); CFArrayRef phoneNums = ABMultiValueCopyArrayOfAllValues(phones); if ([firstLabel isEqualToString:@"#0云助手号码库yunzhushou"]) { for (int j=0; j<CFArrayGetCount(phoneNums); j++) { NSString *phone = (NSString*)CFArrayGetValueAtIndex(phoneNums, j); if ([phone isEqualToString:phoneNumber]) { ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutableCopy(phones); done = ABMultiValueRemoveValueAndLabelAtIndex(multiPhone,j); ABRecordSetValue(record, kABPersonPhoneProperty, multiPhone, nil); CFRelease(multiPhone); break; }else{ NSLog(@"没有找到要删除的号码:%@",phoneNumber); } } }else { NSLog(@"没有找到要删除的联系人:%@",phoneNumber); } } successBlock(done); ABAddressBookSave(addressBook, nil); CFRelease(addressBook); }); }); }
#pragma mark 增加一个号码属性 - (void)addPersonPropertyWithNumberModel:(NumberModel *)numberModel withBlock:(void(^)(BOOL success))successBlock{ dispatch_async(dispatch_get_global_queue(0, 0), ^{ __block bool done = NO; ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef records; // 获取通讯录中全部联系人 records = ABAddressBookCopyArrayOfAllPeople(addressBook); // 遍历全部联系人,检查是否存在指定号码再进行add bool have = NO; for (int i=0; i<CFArrayGetCount(records); i++) { ABRecordRef record = CFArrayGetValueAtIndex(records, i); CFTypeRef phones = ABRecordCopyValue(record, kABPersonPhoneProperty); NSString *firstLabel = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones,0)); NSLog(@"firstLabel = %@",firstLabel); CFStringRef firstNameRef = ABRecordCopyValue(record, kABPersonFirstNameProperty); CFStringRef lastName = ABRecordCopyValue(record, kABPersonLastNameProperty); //此处正常应该根据名字判断 下面是特殊情况根据首个号码标签属性来判断 if ([firstLabel isEqualToString:@"#0云助手号码库yunzhushou"]) { have = YES; BOOL containNumber = NO; //存在 本地联系人只要添加一对标签号码属性 //已存在则不添加 CFArrayRef phoneNumbers = ABMultiValueCopyArrayOfAllValues(phones); for (int j ; j < CFArrayGetCount(phoneNumbers); j++) { NSString *number = (__bridge NSString *)CFArrayGetValueAtIndex(phoneNumbers, j); if ([number isEqualToString:numberModel.phone]) { containNumber = YES; break; } } if (!containNumber) { ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutableCopy(phones); ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFTypeRef)(numberModel.phone), (__bridge CFStringRef)(numberModel.rpt_type), NULL); ABRecordSetValue(record, kABPersonPhoneProperty, multiPhone, nil); CFRelease(multiPhone); done = ABAddressBookSave(addressBook, nil); NSLog(@"本地标记联系人存在 只是添加属性"); break ; } } } if (!have) { //不存在 首次添加 NSLog(@"本地标记联系人不存在 首次添加"); // [self wirteLocalPhoneNumbersToContactFromDB]; done = YES; } ABAddressBookSave(addressBook, nil); CFRelease(addressBook); successBlock(done); }); }
//iOS9 组织多号码以备写入新的联系人 - (void)numbersReadyWriteToContact{ CNMutableContact *mContact = [[CNMutableContact alloc]init]; mContact.imageData = UIImagePNGRepresentation([UIImage imageNamed:@""]); mContact.givenName = @""; mContact.familyName = @""; NSString *label = @"住宅"; NSString *number = 0755-83212344; NSMutableArray *arrPhones =[NSMutableArray array]; CNLabeledValue *phone = [CNLabeledValue labeledValueWithLabel:label value:[CNPhoneNumber phoneNumberWithStringValue:number]]; [arrPhones addObject:phone]; mContact.phoneNumbers = arrPhones; [self stroreContact: contact]; } //iOS9 存入新联系人 -多值 - (void)stroreContact:(CNMutableContact *)contact { BOOL existPhone = NO; if (contact.phoneNumbers.count>0) { CNLabeledValue *firstLabelValue = contact.phoneNumbers[0]; CNPhoneNumber *firstPhoneNumber = firstLabelValue.value; existPhone = [self existPhone:firstPhoneNumber.stringValue]; } if (existPhone) { NSLog(@"号码已存在"); return; } NSLog(@"号码不存在,存储新的联系人"); //创建添加请求 CNSaveRequest *saveRequest = [[CNSaveRequest alloc]init]; [saveRequest addContact:contact toContainerWithIdentifier:nil]; //写入 CNContactStore *store = [[CNContactStore alloc]init]; [store executeSaveRequest:saveRequest error:nil]; }
//phoneList 为包含多对号码属性字典的数组,用于一个为一个联系人添加多个号码- (void)addNewContact:(NSArray *)phonesList { BOOL existPhone = NO; if (phonesList.count>1) { NSString *firstPhoneNumber = phonesList[1][@"value"]; existPhone = [self existPhone:firstPhoneNumber]; } if (existPhone) { NSLog(@"号码已存在"); return; } NSLog(@"号码不存在,存储新的联系人"); //name ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate(); CFErrorRef error = NULL; ABRecordRef newPerson = ABPersonCreate(); ABRecordSetValue(newPerson, kABPersonFirstNameProperty, @"", &error); ABRecordSetValue(newPerson, kABPersonLastNameProperty, @"", &error); ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType); for (NSInteger i = 0; i < phonesList.count; i++) { ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFTypeRef)(phonesList[i][@"value"]), (__bridge CFStringRef)(phonesList[i][@"label"]), NULL); ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone, nil); NSLog(@"%ld",i); } ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error); ABAddressBookSave(iPhoneAddressBook, &error); CFRelease(multiPhone); CFRelease(newPerson); CFRelease(iPhoneAddressBook); }
// 指定号码是否已经存在 - (BOOL)existPhone:(NSString*)phoneNum{ ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef records; // 获取通讯录中全部联系人 records = ABAddressBookCopyArrayOfAllPeople(addressBook); // 遍历全部联系人,检查是否存在指定号码 for (int i=0; i<CFArrayGetCount(records); i++) { ABRecordRef record = CFArrayGetValueAtIndex(records, i); CFTypeRef phones = ABRecordCopyValue(record, kABPersonPhoneProperty); CFArrayRef phoneNums = ABMultiValueCopyArrayOfAllValues(phones); if (phoneNums) { for (int j=0; j<CFArrayGetCount(phoneNums); j++) { NSString *phone = (NSString*)CFArrayGetValueAtIndex(phoneNums, j); if ([phone isEqualToString:phoneNum]) { return YES; } } } } CFRelease(addressBook); return NO; }
时间: 2024-09-30 07:40:43