AddressBookUI.Framwork应用之ABPersonViewController, ABUnknownPersonViewController,ABNewPersonViewContro

设置委托 ABPersonViewControllerDelegate,personVc.personViewDelegate=self;

//显示和编辑一个联系人信息

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

    //把联系人信息作为导航栏的根视图,才可以显示编辑按钮和 完成按钮,返回按钮

    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:personVc];

    ABRecordRef person=ABPersonCreate();

    //CFString=CFSTR("")类似NSString =@"";直接赋值

    ABRecordSetValue(person, kABPersonFirstNameProperty, CFSTR("jun"), NULL);

    ABRecordSetValue(person, kABPersonLastNameProperty, CFSTR("ye"), NULL);

    personVc.personViewDelegate=self;

    personVc.displayedPerson=person;

    CFRelease(person);

    personVc.allowsEditing=YES;


委托方法:

-(BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier

{

    return NO ;//不为默认操作

}


//添加新联系人到通讯录,一般用于通讯录外面的应用,比如说点击某个按钮,直接进入联系人编辑,然后添加到通讯录

ABUnknownPersonViewController *unknown=[[ABUnknownPersonViewController alloc]init];

    unknown.displayedPerson=person;

    unknown.allowsAddingToAddressBook=YES;//允许添加


 //委托方法

-(BOOL)unknownPersonViewController:(ABUnknownPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier

{

    return NO;

}


//新建联系人,允许用户填入一个新联系人的所有属性,一般是通讯录里面的新建

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

    new.newPersonViewDelegate=self;

//委托方法

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

{

    //用于编辑新联系人后做的事件,比如说模态退出,比如说不保存联系人到通讯录

    if (person!=NULL) {

        ABAddressBookRef adbk=ABAddressBookCreate();

        //从数据库中删除新加的联系人

        ABAddressBookRemoveRecord(adbk, person, NULL);

        CFStringRef name=ABRecordCopyCompositeName(person);

        NSLog(@"name%@",name);//对新加的联系人做处理,不一定要保存到通讯录数据库

        CFRelease(name);

        CFRelease(adbk);

        

    }

}





你可能感兴趣的:(AddressBookUI.Framwork应用之ABPersonViewController, ABUnknownPersonViewController,ABNewPersonViewContro)