手机联系人相关AddressBookUI.Framwork和AddressBook.Framework

AddressBook.Framework应用之ABAddressBookRef,ABRecordRef,ABMutableMultiValueRef,ABMultiValueRef

AddressBook.framework是c  api直接访问操作 通讯录数据库框架,AddressBookUI.framework是oc 界面交互框架;

以下代码是操作通讯录,纯c框架没有*指针定义,没有界面交互,只需导入AddressBook.framework框架

 ABAddressBookRef adbk=ABAddressBookCreate();//获取本地通讯录数据库

    ABRecordRef moi=NULL;//联系人

   ABRecordRef annkey=ABPersonCreate();//创建联系人

    //设置联系人的值

    ABRecordSetValue(annkey,kABPersonFirstNameProperty,@"annkey",NULL);

    ABRecordSetValue(annkey,kABPersonLastNameProperty,@"hu",NULL);

     //创建多值属性

    ABMutableMultiValueRef addr=ABMultiValueCreateMutable(kABStringPropertyType);

     //增加属性名和属性值,属性名为kABHomeLabel

    ABMultiValueAddValueAndLabel(addr,@"[email protected]",kABHomeLabel,NULL);

     //设置联系人的多值邮箱属性

   ABRecordSetValue(annkey,kABPersonEmailProperty, addr,NULL);

    

   ABAddressBookAddRecord(adbk, annkey,NULL);//增加联系人

    ABAddressBookSave(adbk,NULL);//保存联系人

    

   CFRelease(addr);

    CFRelease(annkey);//,即使是在arc机制里,c对象仍需手动释放

    

    CFArrayRef sams=ABAddressBookCopyPeopleWithName(adbk, (CFStringRef)@"hu");//联系人数组,可能存在多个同名的联系人,需要通过其他属性来判断具体是哪个

   for (CFIndex ix=0; ix<CFArrayGetCount(sams); ix++) {

        

             // 从联系人数组多个sam中读取

       ABRecordRef sam=CFArrayGetValueAtIndex(sams, ix);

            //  获取联系人的名属性

        CFStringRef last=ABRecordCopyValue(sam,kABPersonLastNameProperty);

       NSLog(@" is find %@",last);

        //找到符合条件的联系人

       if (last&&CFStringCompare(last, (CFStringRef)@"annkey",0)==0) {

            moi=sam;

                   }

       if (last) {

           //必须要判断cf对象是否为空,只有不是null才可被释放,如果不判断会报错。

           CFRelease(last); //c对象需手动释放

        }

        

    }

   if (NULL==moi) {

       //此处通讯录肯定不为空,数据库是存在的

       CFRelease(sams);

       CFRelease(adbk);//c对象需手动释放

       return;

    }

    //获取联系人的邮件属性,初始化为多值

    ABMultiValueRef emails=ABRecordCopyValue(moi,kABPersonEmailProperty);

   if (NULL==emails) {

        NSLog(@"emails is null");

    }

   for (CFIndex ix=0; ix<ABMultiValueGetCount(emails); ix++) {

        //联系人的属性名和属性值

       CFStringRef labe1=ABMultiValueCopyLabelAtIndex(emails, ix);

       CFStringRef value=ABMultiValueCopyValueAtIndex(emails, ix);

        NSLog(@"i have a %@ address I%@",labe1,value);

       CFRelease(labe1);

       CFRelease(value);

    }

    NSLog(@"emails is null2");

   CFRelease(emails);

   CFRelease(sams);

   CFRelease(adbk);


AddressBookUI.Framwork应用之ABPeoplePickerNavigationController

 必须设置委托ABPeoplePickerNavigationControllerDelegate,ab.peoplePickerDelegate=self;

委托方法:

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier

{

    //点击联系人详情里面的属性,对相应的属性做处理

   ABMultiValueRef values=ABRecordCopyValue(person, property);

    CFIndex ix=ABMultiValueGetIndexForIdentifier(values, identifier);

    CFStringRef value=ABMultiValueCopyValueAtIndex(values, ix);

   NSLog(@"%@",value);

   CFRelease(values);

   CFRelease(values);

    //获取某个值的标识器

    CFIndex identifie=ABMultiValueGetIdentifierAtIndex(values, ix);

   NSLog(@"identifier=%ld",identifie);

    //限制用户可以看到的属性

    peoplePicker.displayedProperties=[NSArrayarrayWithObject:[NSNumbernumberWithInt:kABPersonEmailProperty]];

    returnNO;//默认操作为选择进入相应的应用程序,比如打电话就进入电话程序

}

AddressBookUI.Framwork应用之ABPersonViewController, ABUnknownPersonViewController,ABNewPersonViewController

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

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

    ABPersonViewController *personVc=[[ABPersonViewControlleralloc]init];

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

    UINavigationController *nav=[[UINavigationControlleralloc]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=[[ABUnknownPersonViewControlleralloc]init];

    unknown.displayedPerson=person;

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


 //委托方法

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

{

    return NO;

}


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

    ABNewPersonViewController *new=[[ABNewPersonViewControlleralloc]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);

        

    }

}




你可能感兴趣的:(ios,iPhone,AddressBook,AddressBookUI,手机联系人)