ios联系人信息增删改查

来源:互联网 发布:阿里云销售待遇 编辑:程序博客网 时间:2024/04/29 06:49

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

原创粉丝点击