代码在通讯录中插入测试联系人
//修改这里一百条一百条添加 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) { } }); }
初探佳境,多多学习交流