//修改這裏一百條一百條添加 for (int i = 900 ; i<1000; i++) { NSString *phone = [NSString stringWithFormat:@"17601222%03d",i]; NSString *name = [NSString stringWithFormat:@"測試%03d",i]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)0.5*NSEC_PER_SEC), dispatch_get_main_queue(), ^{ NSLog(@"-----%@",phone); [self gotoAddContacts:[self createContactWithStr:phone name:name]]; }); } - (NSMutableDictionary *)createContactWithStr:(NSString*)phone name:(NSString*)name{ NSMutableDictionary *infoDictionary = [NSMutableDictionary dictionaryWithCapacity:0]; [infoDictionary setObject:name forKey:@"name"]; [infoDictionary setObject:phone forKey:@"phone"]; [infoDictionary setObject:@"1000000000@qq.com" forKey:@"email"]; return infoDictionary; } //////////// /** 添加聯繫人 */ - (void)gotoAddContacts:(NSMutableDictionary*)newContact{ //添加到通信錄,判斷通信錄是否存在 if ([self isExistContactPerson:newContact]) {//存在,返回 //提示 if (IS_iOS8) { UIAlertController *tipVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"聯繫人已存在..." preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { [self dismissViewControllerAnimated:YES completion:nil]; }]; [tipVc addAction:cancleAction]; [self presentViewController:tipVc animated:YES completion:nil]; }else{ UIAlertView *tip = [[UIAlertView alloc] initWithTitle:@"提示" message:@"聯繫人已存在..." delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil]; [tip show]; // [tip release]; } return; }else{//不存在 添加 [self creatNewRecord:newContact]; } } - (BOOL)isExistContactPerson:(NSMutableDictionary*)newContact{ //這個變量用於記錄受權是否成功,即用戶是否容許咱們訪問通信錄 int __block tip=0; BOOL __block isExist = NO; //聲明一個通信簿的引用 ABAddressBookRef addBook =nil; //由於在IOS6.0以後和以前的權限申請方式有所差異,這裏作個判斷 if (IS_iOS6) { //建立通信簿的引用,第一個參數暫時寫NULL,官方文檔就是這麼說的,後續會有用,第二個參數是error參數 CFErrorRef error = NULL; addBook=ABAddressBookCreateWithOptions(NULL, &error); //建立一個初始信號量爲0的信號 dispatch_semaphore_t sema=dispatch_semaphore_create(0); //申請訪問權限 ABAddressBookRequestAccessWithCompletion(addBook, ^(bool greanted, CFErrorRef error) { //greanted爲YES是表示用戶容許,不然爲不容許 if (!greanted) { tip=1; }else{ //獲取全部聯繫人的數組 CFArrayRef allLinkPeople = ABAddressBookCopyArrayOfAllPeople(addBook); //獲取聯繫人總數 CFIndex number = ABAddressBookGetPersonCount(addBook); //進行遍歷 for (NSInteger i=0; i<number; i++) { //獲取聯繫人對象的引用 ABRecordRef people = CFArrayGetValueAtIndex(allLinkPeople, i); //獲取當前聯繫人名字 NSString*firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty)); if ([firstName isEqualToString:[newContact objectForKey:@"name"]]) { isExist = YES; } } } //發送一次信號 dispatch_semaphore_signal(sema); }); //等待信號觸發 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); }else{ //IOS6以前 addBook =ABAddressBookCreate(); //獲取全部聯繫人的數組 CFArrayRef allLinkPeople = ABAddressBookCopyArrayOfAllPeople(addBook); //獲取聯繫人總數 CFIndex number = ABAddressBookGetPersonCount(addBook); //進行遍歷 for (NSInteger i=0; i<number; i++) { //獲取聯繫人對象的引用 ABRecordRef people = CFArrayGetValueAtIndex(allLinkPeople, i); //獲取當前聯繫人名字 NSString*firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty)); if ([firstName isEqualToString:[newContact objectForKey:@"name"]]) { isExist = YES; } } } if (tip) { //設置提示 if (IS_iOS8) { UIAlertController *tipVc = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"請您設置容許APP訪問您的通信錄\nSettings>General>Privacy" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { [self dismissViewControllerAnimated:YES completion:nil]; }]; [tipVc addAction:cancleAction]; [tipVc presentViewController:tipVc animated:YES completion:nil]; }else{ UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"友情提示" message:@"請您設置容許APP訪問您的通信錄\nSettings>General>Privacy" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil]; [alart show]; //非ARC // [alart release]; } } return isExist; } //建立新的聯繫人 - (void)creatNewRecord:(NSMutableDictionary*)newContact{ CFErrorRef error = NULL; //建立一個通信錄操做對象 ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error); //建立一條新的聯繫人紀錄 ABRecordRef newRecord = ABPersonCreate(); //爲新聯繫人記錄添加屬性值 ABRecordSetValue(newRecord, kABPersonFirstNameProperty, (__bridge CFTypeRef)[newContact objectForKey:@"name"], &error); //建立一個多值屬性(電話) ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType); ABMultiValueAddValueAndLabel(multi, (__bridge CFTypeRef)[newContact objectForKey:@"phone"], kABPersonPhoneMobileLabel, NULL); ABRecordSetValue(newRecord, kABPersonPhoneProperty, multi, &error); //添加email ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType); ABMultiValueAddValueAndLabel(multiEmail, (__bridge CFTypeRef)([newContact objectForKey:@"email"]), kABWorkLabel, NULL); ABRecordSetValue(newRecord, kABPersonEmailProperty, multiEmail, &error); //添加記錄到通信錄操做對象 ABAddressBookAddRecord(addressBook, newRecord, &error); //保存通信錄操做對象 ABAddressBookSave(addressBook, &error); //經過此接口訪問系統通信錄 ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { if (granted) { } }); }