Use of IOS Address Book Development AddressBook Addressbookui Framework

Source: Internet
Author: User

The address book used in the system framework Addressbook,addressbookui;ios is stored in the database, the developer does not have direct access to the Address Book database, and must rely on the API provided by AddressBook to implement the Address book operation. Address Book information can be manipulated through addressbook.framework, but the AddressBook framework is written in C and cannot be used to manage memory using arc, and it needs to be managed manually.

Common types of AddressBook frameworks:

Abaddressbookref: Represents the Address Book object. With this object, you can access and save your address book information directly.

Abrecordref: Represents a common record object, commonly used. As a contact, the object completely records contact information (name, gender, phone, mail), each abrecordref has a unique ID, which can be obtained through Abrecordgetrecordid ().

Abpersonref: On behalf of contact information, rarely used, the actual development often use Kabpersontype abpersonref to represent the contact person;

Abgrounpref: Represents a group.

The key to the operation of the Address book is the operation of the Abrecordref, which is commonly used as follows:

Abpersoncreate (): Create a abrecordref of type "Kabpersontype";

Abrecordcopyvalue (): Get Contact Properties: Properties of the Official document:

Because of the large number of contact attributes that are involved when a contact is accessed (read, set, delete), you can query to ABPerson.h or go directly to the Help document "Personal information properties"

Abrecordsetvalue (): Sets the Abrecordref property value. Divided into single attributes and multiple attributes, single attribute using Abrecordsetvalue (); Multiple attributes by creating a variable of type abmutablemultivalueref, and then by Abmultivalueaddvalueandlabel The () method adds the property value in turn, and finally sets the variable of type abmutablemultivalueref to the record value through the Abrecordrefsetvalue () method.

Abrecordrmovevalue (): Deletes the specified property value.

Note: The object being fetched is a CF object, the compiler cannot be freed automatically and can be converted to NSObject object by bridge-casts, such as Nsstring*firstname= (__bridge nsstring*) Abrecordcopyvalue (person, kabpersonfirstnameproperty); bridge-casts can refer to this article http://blog.csdn.net/chengwuli125/ article/details/25497051.

It is recommended to use Cfbridgingrelease () to transfer ownership from the Core Foundation to Objective-c;

Use Cfbridgingretain () to transfer ownership from Objective-c to Core Foundation so that you do not have to manually release the CF object.

The above is an introduction to AddressBook framework objects and methods, followed by reading the contact, the project is the simplest to call the system of the Addressbookui framework.

First, use Abpeoplepickernavigationcontroller limitations: Only one contact can be selected, and the page cannot be customized. Pros: Quick and easy, the view itself inherits from the Uinavigationcotroller does not need to use the Uinavigationcontroller package.

Can be launched as a modal view.

Abpeoplepickernavigationcontroller*ppvc=[[abpeoplepickernavigationcontroller Alloc]init];

ppvc.peoplepickerdelegate=self;//Setting up Agents

[Self.navigationcontroller PRESENTVIEWCONTROLLER:PPVC Animated:yes Completion:nil];

Method: Agent Abpeoplepickernavigationcontrollerdelegate;

Called after a is a person with been selected by the user.

IOS8 method, select the action after the contact,

-(void) Peoplepickernavigationcontroller: (abpeoplepickernavigationcontroller*) peoplepicker DidSelectPerson: ( ABRECORDREF) person Ns_available_ios (8_0);

The person property can be processed according to your needs.

Name: Nsstring*firstname= (__bridgensstring*) abrecordcopyvalue (person, kabpersonfirstnameproperty);

Phone: abmultivalueref phones=abrecordcopyvalue (person, kabpersonphoneproperty);

for (int i=0;i<abmultivaluegetcount (phones); i++)

{

Nsstring*phone= (__bridge NSString *) abmultivaluecopyvalueatindex (phones, i);

[_PhoneNumber Addobject:phone];

}

After the call is taken out as an array, there may be more than one.

Called after a is been selected by the user.

IOS8 the method after the Contact property is selected, if the previous method implementation of this method is not called.

-(void) Peoplepickernavigationcontroller: (abpeoplepickernavigationcontroller*) peoplepicker DidSelectPerson: ( ABRECORDREF): (Abpropertyid) Property identifier: (abmultivalueidentifier) identifier Ns_available_ios (8_0);

Called after the user has pressed cancel.

Click the action after canceling

-(void) Peoplepickernavigationcontrollerdidcancel: (Abpeoplepickernavigationcontroller *) PeoplePicker;

Note: Select a contact or click Cancel.

[Self dismissviewcontrolleranimated:yes completion:nil];

The following is the previous method, the iOS8 on the selection of contacts will enter the contact details page, do not read the contact information, use with caution.

Deprecated, use Predicateforselectionofperson and/or-peoplepickernavigationcontroller:didselectperson:instead.

-(BOOL) Peoplepickernavigationcontroller: (Abpeoplepickernavigationcontroller *) peoplepicker Shouldcontinueafterselectingperson: (abrecordref) person Ns_deprecated_ios (2_0, 8_0);

, use Predicateforselectionofproperty And/or-peoplepickernavigationcontroller:didselectperson:property: Identifier:instead.

-(BOOL) Peoplepickernavigationcontroller: (Abpeoplepickernavigationcontroller *) peoplepicker Shouldcontinueafterselectingperson: (abrecordref): (Abpropertyid) Property identifier: ( Abmultivalueidentifier) identifier Ns_deprecated_ios (2_0, 8_0);

Second, Addressbookui also provides abpersonviewcontroller: used to view contact information (need to set Displayedperson properties to display or need to edit the contact);

Abnewpersonviewcontroller: For new contacts.

Abunknownpersonviewcontroller: Used to display unknown contacts.

The above are inherited from Uiviewcontroller in the process must use a uinavigationcontroller for encapsulation, otherwise you can only see the view, unable to operate. After encapsulation, you do not have to deal with the specific additions, modify the contact's logic, but you must handle the shutdown operation to invoke the Dismissviewcontrolleranimated method.

More often than not, the developer will customize the Address book, which is developed through the AddressBook framework.

Development steps: 1, create Address Book Object Abaddressbookref Abaddressbookcreatewithopio ();

2, obtain the user authorizes the Access Address Book abaddressbookrrequestaccesswithcompletion ();

3, Inquiries contact information abaddressbookcopyarrayofallpeople (), Abaddressbookcopypeoplewithname ();

4, after reading the contact person if you want to display contact information, you can call Abrecord related methods to read the corresponding data, if you want to modify the contact information, you can use the corresponding method to modify the Abrecord information, and then call the Abaddressbooksave () method to commit the modification If you want to delete a contact, you can call the Abaddressbookremoverecord () method to delete it, and then call Abaddressbooksave () to commit the modify operation.

5, if you want to modify or delete the need to first query the corresponding contact, and then modify or delete after committing the changes. If the user wants to add a contact instead of querying, call the Abpersoncreate () method directly to create a abrecord and then set the specific property, calling the Abaddressbookaddrecord method to add it.

Read all contact information:

1, Abaddressbookref addressbook=abaddressbookcreatewithoptions (nil, nil);

Cfarrayref allperson =abaddressbookcopyarrayofallpeople (addressbook);

Nsarray *array=cfbridgingrelease (Allperson);

New Contact:

Create a record

Abrecordref recordref= abpersoncreate ();

Abrecordsetvalue (RecordRef, Kabpersonfirstnameproperty, (__bridge cftyperef) (firstName), NULL);//Add Name

Abrecordsetvalue (RecordRef, Kabpersonlastnameproperty, (__bridge cftyperef) (lastName), NULL);//Add last Name

Abmutablemultivalueref multivalueref =abmultivaluecreatemutable (kabstringpropertytype);//Add set multi-valued property

Abmultivalueaddvalueandlabel (Multivalueref, (__bridge cfstringref) (Worknumber), Kabworklabel, NULL);//Add Work Phone

Abrecordsetvalue (RecordRef, Kabpersonphoneproperty, Multivalueref, NULL);

Add a record

Abaddressbookaddrecord (AddressBook, RecordRef, NULL);

Save Contacts, Commit changes

Abaddressbooksave (AddressBook, NULL);

Freeing resources

Cfrelease (RECORDREF);

Cfrelease (MULTIVALUEREF);

3. Delete Contacts by name

Cfstringref personnameref= (__bridge cfstringref) (personname);

Cfarrayref recordsref= abaddressbookcopypeoplewithname (addressbook, personnameref);//Search by person name

Cfindex count= Cfarraygetcount (RECORDSREF);//Acquisition of records

for (Cfindex i=0; i<count; ++i) {

Abrecordref Recordref=cfarraygetvalueatindex (Recordsref, I);//Get the specified record

Abaddressbookremoverecord (Self.addressbook, RecordRef, NULL);//delete

}

Abaddressbooksave (Self.addressbook, NULL);//Commit changes after delete

Cfrelease (RECORDSREF);//Release resources

4. Modify contact information

Abrecordref Recordref=abaddressbookgetpersonwithrecordid (Addressbook,recordid);

Abrecordsetvalue (RecordRef, Kabpersonfirstnameproperty, (__bridge cftyperef) (firstName), NULL);//Add Name

Abrecordsetvalue (RecordRef, Kabpersonlastnameproperty, (__bridge cftyperef) (lastName), NULL);//Add last Name

Abmutablemultivalueref multivalueref =abmultivaluecreatemutable (kabstringpropertytype);

Abmultivalueaddvalueandlabel (Multivalueref, (__bridge cfstringref) (Worknumber), Kabworklabel, NULL);

Abrecordsetvalue (RecordRef, Kabpersonphoneproperty, Multivalueref, NULL);

Save records, Commit changes

Abaddressbooksave (Self.addressbook, NULL);

Freeing resources

Cfrelease (MULTIVALUEREF);

Use of IOS Address Book Development AddressBook Addressbookui Framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.