iOS8通信錄之聯繫人增刪查,多號碼增刪操做

#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; }
相關文章
相關標籤/搜索