IOS添加通讯录

简介: IOS添加通讯录
-(void)touchSaveLocal
{
    //ABAddressBookRef addressBook = ABAddressBookCreate();
    //初始化一个record
    ABRecordRef person = ABPersonCreate();
    ABAddressBookRef addressBook = nil;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
    {
        addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
        //等待同意后向下执行
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error)
                                                 {
                                                     dispatch_semaphore_signal(sema);
                                                 });
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    }
    else
    {
        addressBook = ABAddressBookCreate();
    }
    //这是一个空的记录,或者说是没有任何信息的联系人
    //下面给这个人 添加一个名字
    NSString *firstName = self.detailNode.userName;
    ABRecordSetValue(person, kABPersonFirstNameProperty, (CFStringRef)CFBridgingRetain(firstName), NULL);
    //phone
    ABMultiValueRef phone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(phone, CFBridgingRetain(self.detailNode.phone), kABPersonPhoneMainLabel, NULL);//添加移动号码0
    ABRecordSetValue(person, kABPersonPhoneProperty, phone, NULL);
    //compay
    NSString *gongsi = self.detailNode.company;
    ABRecordSetValue(person, kABPersonOrganizationProperty, (CFStringRef)CFBridgingRetain(gongsi), NULL);
    //将新的记录,添加到通讯录中
    ABAddressBookAddRecord(addressBook, person, NULL);
    //通讯录执行保存
    ABAddressBookSave(addressBook, NULL);
    //不说了,你懂的~
    if(addressBook)
        CFRelease(addressBook);
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"添加成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];}
相关文章
|
存储 Java iOS开发
iOS 通讯录中文排序、全拼音排序
在做项目的时候,总遇到排序问题,英文排序是很简单的,直接使用compare方法就可以实现了,但是一旦遇到中文,就显得麻烦了。
198 0
|
API iOS开发
iOS开发- 添加地址从通讯录中选择添加(firstName,lastName真机为nil,模拟器正常)
iOS开发- 添加地址从通讯录中选择添加(firstName,lastName真机为nil,模拟器正常)
110 0
iOS开发- 添加地址从通讯录中选择添加(firstName,lastName真机为nil,模拟器正常)
|
编解码 自然语言处理 API
iOS小技能:通讯录
iOS处理语言工具CFStringTransform : 智能地处理用户的输入内容,经典应用场景【索引】
78 0
|
iOS开发
iOS调用系统通讯录
iOS调用系统通讯录
194 0
iOS调用系统通讯录
|
iOS开发
iOS获取通讯录联系人信息(二)
iOS获取通讯录联系人信息
124 0
|
iOS开发
iOS获取通讯录联系人信息(一)
iOS获取通讯录联系人信息
381 0