iOS Development Contacts AddressBook

Source: Internet
Author: User

How to access the user's contacts in iOS, there are 2 frames to access the user's address book
    • Addressbookui.framework provides a contact list interface, contact details interface, add contact interface, and so on to select contacts.
    • Addressbook.framework Pure C Language API, just to get contact data does not provide UI interface display, need to build their own contact display interface data types are mostly based on the core Foundation framework

Starting with iOS6, you need to be authorized by the user to access the Address book, so before you use it, you need to check whether the user has authorized

Obtain the authorization status of the Address Book: Abaddressbookgetauthorizationstatus ()

Authorization status
    • Kabauthorizationstatusnotdetermined user has not decided whether to authorize your program to access
    • Kabauthorizationstatusrestricted Parental Controls on an iOS device or some other license configuration blocker to interact with the contacts database
    • Kabauthorizationstatusdenied user explicitly rejects your program's access to your contacts
    • Kabauthorizationstatusauthorized user has authorized your program to access the contacts
Request access to Contacts
1 //instantiating Address Book objects2Abaddressbookref AddressBook =abaddressbookcreatewithoptions (null, NULL);3Abaddressbookrequestaccesswithcompletion (AddressBook, ^ (BOOLgranted, cferrorref error) {4     if(granted) {5NSLog (@"Authorized Success! ");6}Else {7NSLog (@"Authorization failed!");8     }9 });Ten cfrelease (addressbook); One  AHint: the code to request access to the Address book is usually placed in appdelegate
Contact Property Definition

All attribute constant values are defined in the ABPerson.h header file

The contact properties include the following types:

    • Simple properties: Last name, first name, etc.
    • Multiple attributes: Phone number, email, etc.
    • Combination properties: Address, etc.

Note: Using Abrecordcopyvalue can get the corresponding record from a person record, but subsequent processing needs to be differentiated by the specific type of record

Simple properties

A contact is a abrecordref, each contact has its own attributes, such as name, phone, mail, etc.

Use the Abrecordcopyvalue function to get a simple property of a contact from Abrecordref (for example: a string)

Abrecordcopyvalue function receives 2 parameters

    • The 1th parameter is a abrecordref instance
    • The 2nd parameter is the attribute keyword, defined in ABPerson.h

The Abpersoncopylocalizedpropertyname function can get the corresponding label text based on the specified keyword

Get all your contact data
1 //get all Contact records2Cfarrayref array =abaddressbookcopyarrayofallpeople (addressbook);3Nsinteger count =Cfarraygetcount (array);4 5  for(Nsinteger i =0; I < count; ++i) {6     //Take out a record7Abrecordref person =Cfarraygetvalueatindex (array, i);8     9     //Remove the details from your personal recordTen           //name OneCfstringref firstNameLabel =Abpersoncopylocalizedpropertyname (kabpersonfirstnameproperty); ACfstringref FirstName =Abrecordcopyvalue (person, kabpersonfirstnameproperty); -Cfstringref Lastnamelabel =Abpersoncopylocalizedpropertyname (kabpersonlastnameproperty); -     //Surname theCfstringref LastName =Abrecordcopyvalue (person, kabpersonlastnameproperty); -      -NSLog (@"%@ %@ - %@ %@", Lastnamelabel, LastName, firstNameLabel, firstName); -}
Bridging between the Corefoundation and foundation
1 //1. Get the Address Book reference2Abaddressbookref AddressBook =abaddressbookcreatewithoptions (NULL, nil);3 //2. Get all Contact records4Nsarray *array = (__bridge Nsarray *) (Abaddressbookcopyarrayofallpeople (AddressBook));5  for(Nsinteger i =0; i < Array.count; i++) {6     //Take out a record7Abrecordref person =(__bridge abrecordref) (Array[i]);8     //Remove the details from your personal record9NSString *firstnamelabel = (__bridge NSString *) (Abpersoncopylocalizedpropertyname (Kabpersonfirstnameproperty));TenNSString *firstname = (__bridge NSString *) (Abrecordcopyvalue (person, kabpersonfirstnameproperty)); OneNSString *lastnamelabel = (__bridge NSString *) (Abpersoncopylocalizedpropertyname (Kabpersonlastnameproperty)); ANSString *lastname = (__bridge NSString *) (Abrecordcopyvalue (person, kabpersonlastnameproperty)); -NSLog (@"%@ %@ - %@ %@", Lastnamelabel, LastName, firstNameLabel, firstName); - } theCfrelease (AddressBook);
Multiple attributes

Some property values for a contact are not as simple as a property may contain multiple values

    • such as mailbox, divided into work e-mail, residential mailbox, other mailbox, etc.
    • such as telephone, work phone, home phone, other telephone, etc.

If it is a complex attribute, then the Abrecordcopyvalue function returns data of type Abmultivalueref, such as a mailbox or a phone

1 // Pick up phone number 2 abmultivalueref phones = Abrecordcopyvalue (person, kabpersonphoneproperty); 3 // number of records taken 4 Nsinteger phonecount = abmultivaluegetcount (phones); 5 // traverse all the phone numbers 6  for 0; i < Phonecount; i++) {7 }
Ways to get complex properties
1 // phone Tag 2 cfstringref Phonelabel = abmultivaluecopylabelatindex (phones, i); 3 // Localizing Phone Labels 4 cfstringref Phonelocallabel = Abaddressbookcopylocalizedlabel (phonelabel); 5 // Phone number 6 cfstringref phonenumber = abmultivaluecopyvalueatindex (phones, i);
Steps to add a contact

Steps to add a contact

    • Create a new contact with the Abpersoncreate function (return to ABRECORDREF)
    • To set the properties of a contact by using the Abrecordsetvalue function
    • Add contacts to the contacts database by using the Abaddressbookaddrecord function
    • Save the changes you just made by using the Abaddressbooksave function

You can use the Abaddressbookhasunsavedchanges function to determine if there are unsaved changes

When you decide whether to change the Address book database, you can use Abaddressbooksave or Abaddressbookrevert to save or discard changes, respectively.

Steps to add a group

To add a group roughly the same as adding a contact

    • Create a new group with the Abpersoncreate function (return abrecordref)
    • Set group name by Abrecordsetvalue function
    • To add a group to the contacts database by using the Abaddressbookaddrecord function
    • Save the changes you just made by using the Abaddressbooksave function
Manipulate the avatar of the contact person

To manipulate the avatar of a contact, you have the following function

    • Bpersonhasimagedata determine if a contact in the Address book has a picture
    • Abpersoncopyimagedata obtain picture data (if any)
    • Abpersonsetimagedata setting up a contact's picture data

iOS Development Contacts AddressBook

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.