ios联系人信息增删改查

ios6以后,对这个功能做了限制,要申请用户授权,这才对嘛,偷偷摸摸读取我的联系人,我肯定不愿意。

下面说一下具体方法:


1. 引入AddressBook.framework框架, 然后#import <AddressBook/AddressBook.h>

2. 基础知识:

  • 因为这个framework是CoreFoundation里面的,所以不受arc的控制,需要自己手动维护,不要忘记release,否则会有内存泄露的问题。
  • 在APP安装到设备以后,第一次会弹出提示,然后在Settings->Privacy->contacts里面可以选择是否授权。如果把app卸载,或者重新build之后,则不会再出现提示信息。如果想再次看见那个alert,那么就要Settings->General->Reset->Reset Location&Privacy,就可以了。
  • 设置提示的信息,在工程的plist文件中,添加一项key为“Privacy - Contacts Usage Description”,后面的value就会显示在提示中。


3. 下面是读取联系人信息的主要代码:

获取联系人姓名的时候取到的是唯一值,获取电话号码的时候取到的是一个数组,所以要分别做处理,其他信息都有相应的key去对应,详见头文件。

    NSArray *contacts = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(self.addressBook);
    NSMutableString *contactString =[NSMutableString string];
    for (id people in contacts) {
        NSString *firstName = (__bridge NSString *)ABRecordCopyValue((__bridge ABRecordRef)(people), kABPersonFirstNameProperty);
        NSString *lastName = (__bridge NSString *)ABRecordCopyValue((__bridge ABRecordRef)(people), kABPersonLastNameProperty);
        ABMultiValueRef phoneNumbers = ABRecordCopyValue((__bridge ABRecordRef)(people), kABPersonPhoneProperty);
        for (int i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
            NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
        }
        CFRelease(phoneNumbers);
    }
    self.contactListLabel.text = contactString;
    if (_addressBook) {
        CFRelease(_addressBook);
    }

4. 下面是删除联系人的主要代码:

    NSArray *array = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(_addressBook);
    for (id obj in array) {
        ABRecordRef people = (__bridge ABRecordRef)obj;
        NSString *firstName_ab = (__bridge NSString *)ABRecordCopyValue(people, kABPersonFirstNameProperty);
        NSString *lastName_ab = (__bridge NSString *)ABRecordCopyValue(people, kABPersonLastNameProperty);
        if ([firstName_ab isEqualToString:firstName] && [lastName_ab isEqualToString:lastName]) {
            ABAddressBookRemoveRecord(_addressBook, people, NULL);
        }
        CFRelease(people);
    }
    if (ABAddressBookSave(_addressBook, NULL)) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Done" message:@"Success to delete contact!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to delete contact!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
    if (_addressBook) {
        CFRelease(_addressBook);
    }

5. 下面是添加联系人的主要代码:

    NSString *firstName = self.firstNameField.text;
    NSString *lastName = self.lastNameField.text;
    NSString *mobileNumber = self.mobileField.text;
    NSArray *phones = [NSArray arrayWithObjects:mobileNumber, nil];
    NSArray *phoneLabels = [NSArray arrayWithObjects:@"mobile", nil];
    if (firstName.length && lastName.length && mobileNumber.length) {
        ABRecordRef person = ABPersonCreate();
        ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)(firstName), NULL);
        ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)(lastName), NULL);
        ABMultiValueRef mv = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        for (int i = 0; i < [phones count]; i++) {
            ABMultiValueIdentifier mvi =
            ABMultiValueAddValueAndLabel(mv,
                                         (__bridge CFTypeRef)([phones objectAtIndex:i]),
                                         (__bridge CFStringRef)([phoneLabels objectAtIndex:i]),
                                         &mvi);
        }
        ABRecordSetValue(person, kABPersonPhoneProperty, mv, NULL);
        CFRelease(mv);
        ABAddressBookAddRecord(self.addressBook, person, NULL);
        if (ABAddressBookSave(self.addressBook, NULL)) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Done" message:@"Success to save contact!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to save contact!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
    }

    if (_addressBook) {
        CFRelease(_addressBook);
    }

6. update 联系人的方法

用到的方法在上面都已经列出了,所以就不在详述了。


7. 调用contacts.

引入AddressBookUI.framework,然后 #import <AddressBookUI/AddressBookUI.h>, 实现一下ABPeoplePickerNavigationControllerDelegate。

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [peoplePicker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

贴一下调用的代码:

    if (!self.contactPicker) {
        self.contactPicker = [[ABPeoplePickerNavigationController alloc] init];
        self.contactPicker.modalInPopover = YES;
        self.contactPicker.modalPresentationStyle = UIModalPresentationCurrentContext;
        self.contactPicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        self.contactPicker.peoplePickerDelegate = self;
    }
    [self presentViewController:self.contactPicker animated:YES completion:^{
        
    }];

demo 下载地址: http://download.csdn.net/detail/wanghuafeng123456/5570543

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值