IOS: Access Address Book

Source: Internet
Author: User

Access to the Address Book

    • Introduced:

The Address Book is a shared contact information database. Any iOS application is available. You can improve the user experience by providing common contact information instead of having each application manage a separate contact list. After you have a shared address book, you do not need to add contacts to different applications multiple times, and when you update the contact information in one application, all other applications will be able to use them immediately. iOS provides comprehensive address book database access through two frameworks, respectively, in both the addresses books and an address book UI.
The Address Book UI Framework is a set of user-interface classes that encapsulate the framework of the Address Book and provide users with a standard way to use contact information. Use this interface to let users browse, search, and select contacts in the Address Book, display and edit information for selected contacts, and create new contacts.

    • Steps:

<1> preparing for the use of the framework Address Book
To display the Address Book UI and address book data, you must import the header files of the framework's address books and the addresses books UI and indicate that the protocol abpersonpickernavigationcontrollerdelegate will be implemented.
Open the file ViewController.h and add the following line of code after the existing compilation Directive #import:

#import <AddressBook/AddressBook.h>#import <AddressBookUI/AddressBookUI.h>

Next, modify the code @interface, where you add <abpersonpickernavigationcontrollerdelegate> The function is to indicate that we want to abide by the agreement abpersonpickernavigationcontrollerdelegate:

@interface Viewcontroller:uiviewcontroller<abpersonpickernavigationcontrollerdelegate>

<2> Show Address Book contact selector
When the user clicks the Select a button, the application needs to display a modal view of the contact selector, which provides the user with an interface similar to the application's address book. In the file Viewcontroller.m method NEWBFF, assign and initialize a contact selector, set its delegate to view Controller (self), and then in the display. The code for this method is as follows:

//   Open Address Book-(ibaction) NEWBFF:(id*= = Self ; [Self Presentviewcontroller:picker animate:yes];}

<3> handling cancellation and mining
When the contact's interface pops up, you can either select the contact to cancel, or you can continue to delve into the contact's information. Here I just let the user select a friend and then cancel, and do not want the user to continue to select or edit the contact properties. It is therefore necessary to implement the delegate method PeoplePickerNavigationController:peoplePicker:shouldContinueAfterSelectingPerson to return no, which is the core method of this application. You also need to have the delegate method close the contact selector modal view and give the controller to Viewcontroller.
But you must also implement the contact Selector delegate protocol definition as follows:
1. Handling user deselected cases (peoplepickernavigationcontrollerdidcancel)
2. Handle the case where the user is digging deeper into the contact properties (Peoplepickernavigationcontroller:shouldcontinueafterselectingperson: Property:idnetifier).

// Close Address Book-(void) Peoplepickernavigationcontrollerdidcancel: (Abpersonpickernavigationcontroller * ) peoplepicker{[self dissmissviewcontrolleranimate:yes];}

Return the method PeoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:idnetifier implementation to No, This method is used to process the user to deselect in the contact selector, as shown in the following code:

 //  setting does not continue to dig deep contact information -(BOOL) Peoplepickernavigationcontroller: (Abpersonpickernavigationcontroller *) Peoplepicker Shouldcontinueafterselectingperson: (abrecordref) Person property: (Abpropertyid) Property Idntifier: ( Abmultivalueidntifier) idntifier{ // we Won ' t get to this delegate method  return NO;}  

<4> Select, Access, and display contact information
If the user does not deselect, the delegate method PeoplePickerNavigationController:peoplePicker:shouldCotinueAfterSelectingPerson is called, And pass the selected contact to the method through a abrecordref. ABRECORDREF is defined in the AddressBook framework. Here, you will read the four information of the contact's name, photo, e-mail, and zip code, and check whether the contact has a photo before reading it. It is important to note that the returned contact names and photos are not cocoa objects (that is, NSString and uiimage), but rather the C language data in the core Foundation. It is therefore necessary to use the Abrecordcopyvalue and UIImage methods in the AddressBook framework to convert Imagewithdata.
For e-mail addresses and postal codes, you must handle the case of multiple values that may be returned. For this data, you will also use Abrecordcopyvalue to get a reference to the dataset, and then use the function Abmultivaluegetcount to verify that the contact has at least one e-mail address (or zip code). Then use Abmultivalueatindex to copy the first e-mail address or zip code.
Add a delegate method Peoplepickernavigationcontroller:shouldcontinueafterselectingperson in file viewcontroller.m, This method responds when the user selects a contact, with the following code:

-(BOOL) Peoplepickernavigationcontroller: (Abpeoplepickernaivgationcontroller *) Peoplepicker Shouldcontinueafterselectingperson: (abrecordref) person{//Retrieve The friend ' s name from the Address Book person recordNSString *Friendname; NSString*Friendemail; NSString*Friendzip; //get the name of the contact personFriendname = (__bridge NSString *) Abrecordcopyvalue (Person,kabpersonfristnameproperty); NSLog ("Name of contact person:%@", Friendname); //Get contact ZIP codeAbmultivalueref Friendaddressset; Nsdictionary*friendfirstaddress; Friendaddressset=Abrecordcopyvalue (Person,kabpersonaddressproperty); if(Abmultivaluegetcount (Friendaddressset) >0) {friendfirstaddress= (__bridge nsdictionary *) Abrecordcopyvalueatindex (Friendaddressset,0); Friendzip= [Friendfirstaddress Objectforkey:"ZIP"]; NSLog ("Contact ZIP Code:%@", Friendzip); }                        //Get Contact Email addressAbmultivalueref friendemailaddress; Friendemailaddress=Abrecordcopyvalue (Person,kabpersonemailproperty); if(Abmultivaluegetcount (friendemailaddress) >0) {Friendemail= (__bridge NSString *) Abrecordcopyvalueatindex (friendemailaddress,0); NSLog ("Contact e-mail address:%@", Friendemail); }                        //get the image of a contact            if(Abpersonhasimagedata (person)) {UIImage*personimage = [UIImage imagewithdata: (__bridge NSData *) Abpersoncopyimagedata (person)]; NSLog ("image of the contact person:%@", Personimage); }                        //Close Address Book[self dissmissviewcontrolleranimate:yes]; returnNO; }

IOS: Access Address Book

Related Article

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.