iOS通讯录

//新增联系人
-(void)AddPeople{
     //取得本地通信录名柄
    ABAddressBookRef tmpAddressBook = ABAddressBookCreate();
    //创建一条联系人记录
    ABRecordRef tmpRecord = ABPersonCreate();
    CFErrorRef error;
    BOOL tmpSuccess = NO;
    //Nickname
    CFStringRef tmpNickname = CFSTR("Sparky");
    tmpSuccess = ABRecordSetValue(tmpRecord, kABPersonNicknameProperty, tmpNickname, &error);
    CFRelease(tmpNickname);
    //First name
    CFStringRef tmpFirstName = CFSTR("zhang");
    tmpSuccess = ABRecordSetValue(tmpRecord, kABPersonFirstNameProperty, tmpFirstName, &error);
    CFRelease(tmpFirstName);
    //Last name
    CFStringRef tmpLastName = CFSTR("shan");
    tmpSuccess = ABRecordSetValue(tmpRecord, kABPersonLastNameProperty, tmpLastName, &error);
    CFRelease(tmpLastName);   

    //邮箱

    ABMutableMultiValueRef mutableMultiEmails = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(mutableMultiEmails, (__bridge CFTypeRef)(@"22222222@qq.com"), kABPersonPhoneMainLabel, NULL);
    tmpSuccess = ABRecordSetValue(tmpRecord, kABPersonEmailProperty, mutableMultiEmails, &error);

    //地址
    ABMutableMultiValueRef mutableMultiAddress = ABMultiValueCreateMutable(kABDictionaryPropertyType);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:@"湖南长沙" forKey:(__bridge  NSString *) kABPersonAddressStreetKey];
     ABMultiValueAddValueAndLabel(mutableMultiAddress, (__bridge CFTypeRef)(dic), kABHomeLabel, NULL);
     tmpSuccess = ABRecordSetValue(tmpRecord, kABPersonAddressProperty, mutableMultiAddress, &error);

    //手机号
    CFTypeRef tmpPhones = CFSTR("13902400000");
    ABMutableMultiValueRef tmpMutableMultiPhones = ABMultiValueCreateMutable(kABPersonPhoneProperty);
    ABMultiValueAddValueAndLabel(tmpMutableMultiPhones, tmpPhones, kABPersonPhoneMobileLabel, NULL);
    tmpSuccess = ABRecordSetValue(tmpRecord, kABPersonPhoneProperty, tmpMutableMultiPhones, &error);
    CFRelease(tmpPhones);
    //保存记录
    tmpSuccess = ABAddressBookAddRecord(tmpAddressBook, tmpRecord, &error);
    CFRelease(tmpRecord);
    //保存数据库
    tmpSuccess = ABAddressBookSave(tmpAddressBook, &error);
    CFRelease(tmpAddressBook);
}

//读取所有联系人
-(void)ReadAllPeoples{       
        //取得本地通信录名柄
        ABAddressBookRef tmpAddressBook = ABAddressBookCreate();
        //取得本地所有联系人记录
        NSArray* tmpPeoples = (NSArray*)ABAddressBookCopyArrayOfAllPeople(tmpAddressBook);
        for(id tmpPerson in tmpPeoples)
        {               
                //获取的联系人单一属性:First name
                NSString* tmpFirstName = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonFirstNameProperty);
                NSLog(@"First name:%@", tmpFirstName);
                [tmpFirstName release];
                //获取的联系人单一属性:Last name
                NSString* tmpLastName = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonLastNameProperty);
                NSLog(@"Last name:%@", tmpLastName);
                [tmpLastName release];
                //获取的联系人单一属性:Nickname
                NSString* tmpNickname = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonNicknameProperty); 
                NSLog(@"Nickname:%@", tmpNickname);
                [tmpNickname release];
                //获取的联系人单一属性:Company name
                NSString* tmpCompanyname = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonOrganizationProperty);
                NSLog(@"Company name:%@", tmpCompanyname);
                [tmpCompanyname release];
                //获取的联系人单一属性:Job Title
                NSString* tmpJobTitle= (NSString*)ABRecordCopyValue(tmpPerson, kABPersonJobTitleProperty);
                NSLog(@"Job Title:%@", tmpJobTitle);
                [tmpJobTitle release];
                //获取的联系人单一属性:Department name
                NSString* tmpDepartmentName = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonDepartmentProperty);
                NSLog(@"Department name:%@", tmpDepartmentName);
                [tmpDepartmentName release];
                //获取的联系人单一属性:Email(s)
                ABMultiValueRef tmpEmails = ABRecordCopyValue(tmpPerson, kABPersonEmailProperty);
                for(NSInteger j = 0; ABMultiValueGetCount(tmpEmails); j++)
                {
                        NSString* tmpEmailIndex = (NSString*)ABMultiValueCopyValueAtIndex(tmpEmails, j);
                        NSLog(@"Emails%d:%@", j, tmpEmailIndex);
                        [tmpEmailIndex release];
                }
                CFRelease(tmpEmails);
                //获取的联系人单一属性:Birthday
                NSDate* tmpBirthday = (NSDate*)ABRecordCopyValue(tmpPerson, kABPersonBirthdayProperty);
                NSLog(@"Birthday:%@", tmpBirthday);       
                [tmpBirthday release];
                //获取的联系人单一属性:Note
                NSString* tmpNote = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonNoteProperty);
                NSLog(@"Note:%@", tmpNote);       
                [tmpNote release];
                //获取的联系人单一属性:Generic phone number
                ABMultiValueRef tmpPhones = ABRecordCopyValue(tmpPerson, kABPersonPhoneProperty);
                for(NSInteger j = 0; j < ABMultiValueGetCount(tmpPhones); j++)
                {
                        NSString* tmpPhoneIndex = (NSString*)ABMultiValueCopyValueAtIndex(tmpPhones, j);
                        NSLog(@"tmpPhoneIndex%d:%@", j, tmpPhoneIndex);
                        [tmpPhoneIndex release];
                }
                CFRelease(tmpPhones);

    //读取地址多值
    ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty); 
    int count = ABMultiValueGetCount(address);    
    for(int j = 0; j < count; j++)
    { //获取地址Label
        NSString* addressLabel = (NSString*)ABMultiValueCopyLabelAtIndex(address, j);
        //获取該label下的地址6属性
        NSDictionary* addressDic =(NSDictionary*) ABMultiValueCopyValueAtIndex(address, j);        
        NSString* country = [addressDic valueForKey:(NSString *)kABPersonAddressCountryKey];            
        NSString* city = [addressDic valueForKey:(NSString *)kABPersonAddressCityKey];
        NSString* state = [addressDic valueForKey:(NSString *)kABPersonAddressStateKey];            
        NSString* street = [addressDic valueForKey:(NSString *)kABPersonAddressStreetKey];           
        NSString* zip = [addressDic valueForKey:(NSString *)kABPersonAddressZIPKey]; 
        NSString* coutntrycode = [addressDic valueForKey:(NSString *)kABPersonAddressCountryCodeKey];            
    }

        }
        //释放内存
        [tmpPeoples release];
        CFRelease(tmpAddressBook);
}

//删除联系人
-(void)DeletePeople
{
        //取得本地通信录名柄
        ABAddressBookRef tmpAddressBook = ABAddressBookCreate();
        NSArray* tmpPersonArray = (NSArray*)ABAddressBookCopyArrayOfAllPeople(tmpAddressBook);
        for(id tmpPerson in tmpPersonArray)
        {
                NSString* tmpFirstName = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonFirstNameProperty);
                NSString* tmpLastName  = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonLastNameProperty);
                NSString* tmpFullName = [NSString stringWithFormat: @"%@%@", [tmpFirstName lowercaseString], [tmpLastName lowercaseString]];
                [tmpFirstName release];
                [tmpLastName release];
                //删除联系人
                if([tmpFullName isEqualToString:@"zhangshan"])
                {
                        ABAddressBookRemoveRecord(tmpAddressBook, tmpPerson, nil);
                }
        }
        //保存电话本
        ABAddressBookSave(tmpAddressBook, nil);
        //释放内存
        [tmpPersonArray release];
        CFRelease(tmpAddressBook);
}

转载于:https://my.oschina.net/yyyyu/blog/637739

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值