调用系统通讯录

在工程中加入:AddressBook.framework

头文件中引入:#import

//地址簿

@property (nonatomic, assign) ABAddressBookRef addressBook;

@property (nonatomic, strong) NSMutableArray *addressBookEntryArray;

@property(nonatomic, assign) ABRecordRef displayedPerson;

//添加到通讯录

if(buttonIndex == 3){

NgnContact *contact = [[NgnEngine sharedInstance].contactService getContactByLocalNumber:dialNum];

[self editContact:contact];

return;

}

- (void)editContact:(NgnContact *)contact

{

if (contact){ //判断通讯录中是否已经存在该电话号码

//使用ABPersonViewController编辑和查看联系人

ABAddressBookRef addressBook = ABAddressBookCreate();

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);

ABRecordRef person = nil;

person = ABAddressBookGetPersonWithRecordID(addressBook, [contact myid]);

ABPersonViewController *myABPersonViewController = [[ABPersonViewController alloc] init];

myABPersonViewController.displayedPerson = person;

myABPersonViewController.addressBook = addressBook;

[myABPersonViewController setAllowsEditing:YES];

[myABPersonViewController setEditing:YES];

myABPersonViewController.allowsActions = YES;

myABPersonViewController.personViewDelegate = self;

CFRelease(allPeople);

CFRelease(addressBook);

myABPersonViewController.hidesBottomBarWhenPushed = YES;

[self.navigationController pushViewController:myABPersonViewController animated:YES];

}else{

//使用ABNewPersonViewController新建联系人

ABNewPersonViewController *newPersonController = [[ABNewPersonViewController alloc] init];

newPersonController.newPersonViewDelegate = self;

newPersonController.hidesBottomBarWhenPushed = YES;

CFErrorRef error =NULL;

//打开数据库

ABRecordRef personRef=ABPersonCreate();

if (self.shopDetailModel.name.length) {

//添加单个项的属性值,如姓、名、生日、职务、公司...

ABRecordSetValue(personRef, kABPersonFirstNameProperty, (__bridge CFTypeRef)self.shopDetailModel.name, &error);

}

//用于存放具有多个值的项

ABMutableMultiValueRef multi=ABMultiValueCreateMutable(kABMultiStringPropertyType);

//电话号码属于具有多个值的项(除此还有email、地址类)

ABMultiValueAddValueAndLabel(multi,(__bridge CFTypeRef)phone, kABPersonPhoneMobileLabel, NULL);

ABRecordSetValue(personRef, kABPersonPhoneProperty, multi, &error);

newPersonController.displayedPerson = personRef;

CFRelease(multi);

CFRelease(personRef);

[self.navigationController pushViewController:newPersonController animated:YES];

}

}

#pragma mark - ABPersonViewController Delegate

- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person

property:(ABPropertyID)property

identifier:(ABMultiValueIdentifier)identifier

{

return true;

}

#pragma mark - ABnewPersonViewController Delegate

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person {

if (person) {

CFErrorRef error = NULL;

ABAddressBookAddRecord(_addressBook, person, &error);

ABAddressBookSave(_addressBook, &error);

if (error != NULL) {

NSLog(@"An error occurred");

}

}

_addressBookEntryArray = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(_addressBook);

[self.tableView reloadData];

[self.navigationController popToRootViewControllerAnimated:YES];

}

http://my.oschina.net/are1OfBlog/blog/466617

其他详解:

CFErrorRef error =NULL;

//打开数据库

ABRecordRef personRef=ABPersonCreate();

//添加单个项的属性值,如姓、名、生日、职务、公司...

ABRecordSetValue(personRef, kABPersonFirstNameProperty,CFSTR("姓名"), &error);

//如果lastname为空,则不要使用此句,否则电话薄排序显示会有问题,会将其归入#那类中

//ABRecordSetValue(person, kABPersonLastNameProperty, CFSTR(""), &error);

//用于存放具有多个值的项

ABMutableMultiValueRef multi=ABMultiValueCreateMutable(kABMultiStringPropertyType);

//电话号码属于具有多个值的项(除此还有email、地址类)

ABMultiValueAddValueAndLabel(multi, CFSTR("135024"), kABPersonPhoneMobileLabel, NULL);

ABMultiValueAddValueAndLabel(multi, CFSTR("87366227"), kABPersonPhoneMainLabel, NULL);

ABMultiValueAddValueAndLabel(multi, CFSTR("186020389"), kABPersonPhoneIPhoneLabel, NULL);

ABRecordSetValue(personRef, kABPersonPhoneProperty, multi, &error);

//清空该变量用于存放下一个多值的项

multi=nil;

//email也属于具有多个项的值

multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);

ABMultiValueAddValueAndLabel(multi,CFSTR("[email protected]"), kABWorkLabel, NULL);

ABRecordSetValue(personRef, kABPersonEmailProperty, multi, &error);

ABRecordSetValue(personRef, kABPersonOrganizationProperty, CFSTR("部门树"), &error);

ABRecordSetValue(personRef, kABPersonJobTitleProperty, CFSTR("职务"), &error);

//ABRecordSetValue(person, kABPersonNoteProperty, CFSTR("备注"), &error);

//添加并保存到地址本中

ABAddressBookRef addressbookRef=ABAddressBookCreate();

ABAddressBookAddRecord(addressbookRef, personRef, &error);

ABAddressBookSave(addressbookRef, &error);

CFRelease(multi);

CFRelease(personRef);

CFRelease(addressbookRef);

以上是直接保存到联系人了并不会打开ABNewPersonViewController

如果需要打开ABNewPersonViewController进行再次编辑可以加入

ABNewPersonViewController *npvc = [[[ABNewPersonViewController alloc] init] autorelease];

npvc.navigationItem.leftBarButtonItem = BARBUTTON(@"取消", @selector(addNewBackAction:));

self.aBNewPersonNav = [[[UINavigationController alloc] initWithRootViewController:npvc] autorelease];

CFErrorRef error =NULL;

ABRecordRef personRef=ABPersonCreate();

ABMutableMultiValueRef multi=ABMultiValueCreateMutable(kABMultiStringPropertyType);

//电话号码属于具有多个值的项(除此还有email、地址类)

ABMultiValueAddValueAndLabel(multi, CFSTR("135024"), kABPersonPhoneMobileLabel, NULL);

ABRecordSetValue(personRef, kABPersonPhoneProperty, multi, &error);

npvc.displayedPerson = personRef;

npvc.newPersonViewDelegate = self;

[self presentModalViewController:aBNewPersonNav animated:YES];

你可能感兴趣的:(调用系统通讯录)