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;
}
时间: 2024-09-30 07:40:43

iOS8通讯录之联系人增删查,多号码增删操作的相关文章

ESql标签(对数据库进行增删查改及命令操作)

功能:WebEasy的核心功能之一,作为平台持久层的接口,用于对数据库的所有操作 ·        对数据库进行增删查改及命令操作 查询时会返回一个书包(Bag) 别名为EasySql 用法:<esql module=模块名 [id=书包名] [act=edit|exe|batch|refresh] [p] [commit=true|false|end]>SQL语句</esql> module:连接数据库的模块名,必选项,在WebEasy系统目录下(如D:/webeasy),可以建

nodejs连接mysql并进行简单的增删查改

最近在入门nodejs,正好学习到了如何使用nodejs进行数据库的连接,觉得比较重要,便写一下随笔,简单地记录一下 使用在安装好node之后,我们可以使用npm命令,在项目的根目录,安装nodejs中的mysql模块 npm install mysql 在连接数据库之前,要先引入nodejs连接处理mysql的模块 var mysql = require('mysql'); 类似php连接mysql的方式,编写连接代码 //使用nodejs处理mysql的模块,使用创建连接方法,创建与mysq

Python对MySQL进行增删查改

python连接MySQL数据库:pymysql # 测试操作 import pymysql # 打开数据库 db = pymysql.connect("localhost", "root", "test1234", "pythontest", charset='utf8' ) # 使用cursor()方法获取操作游标 cursor = db.cursor() # 使用execute执行sql语句 cursor.execute

ASP.Net增删查改写的一个通讯录系统---ShinePans

1.开发环境 vs 2013,sqlserver 2012,win 8.1x64,C#语言,.net 4.5 2.更能说明 通讯录人性化的增删查改 3.网页效果 1.主页: 2. 查看全部联系人 3.查找职业为学生的联系人: 4.查找指定姓名的联系人: 5.添加联系人: 6.添加后: 7.更新联系人: 8.更新前: 9.更新后: 10.删除联系人: 11.删除前: 12.删除后: 13.关于: 4.关键代码: default.aspx: <%@ Page Title="Delete&quo

iOS8通讯录信息读取兼容

项目中有一个功能需要读取通讯录中联系人的手机.在iOS8以前都是可用的,主要使用如下三个代理方法来实现 - (void) peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker - (BOOL) peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker sho

php mysql增删查改

php mysql增删查改代码段 $conn=mysql_connect('localhost','root','root');  //连接数据库代码 mysql_query("set names utf8");  //传输编码 mysql_query('sql'$conn); //查找名为sql的数据库 admin为表名: 查找数据代码段: $sql="select * from admin ";  //查询表: $sql="select  * from

c++中的顺序表写法,主要实现(增删查改,构造函数,运算符重载)

本文的内容主要是,利用c++写出顺序表,并对写出的代码进行测试, 主要实现的功能:实现对顺序表的增删查改, 要写的函数:构造函数,赋值运算符重载,析构函数.在编写代码过程中应注意到深浅拷贝问题. 下面是顺序表的类函数: #pragma once #include<iostream> using namespace std; typedef int DataType; class SeqList { public: SeqList(); SeqList(DataType *array, size

mysql基础知识之增删查改使用介绍

 mysql基础知识之增删查改使用介绍 本文主要介绍mysql常用的SELECT.INSERT.UPDATE.DELETE语句的使用,数据库的安装这里不做介绍,并且事先已经准备好相关数据. 本文中使用的student表中的数据如下: mysql> SELECT * FROM student; 一.SELECT语句的使用 1.单表查询 语法格式: SELECT culom1,culom2,culom3 FROM TABLE_NAME; 可以改变字段显示顺序 2.WHERE子句查询 语法格式: SE

EF实现增删查改功能

In the previous tutorial you created an MVC application that stores and displays data using the Entity Framework and SQL Server LocalDB. In this tutorial you'll review and customize the CRUD (create, read, update, delete) code that the MVC scaffoldin