IOS ----- use AddressBook to manage contacts, iosaddressbook

Source: Internet
Author: User

IOS ----- use AddressBook to manage contacts, iosaddressbook
Use AddressBook to manage contacts

IPhone mobile phones are usually built-in Contacts applications, including the last name, first name, phone number, e-mail address, address, birthday and other personal information of all Contacts. the Contacts data is stored in the sandbox of your own application. Other applications cannot access them.

IOS provides the following two frameworks to access the contact information stored on the iPhone.

AddressBook. framework

Through the series of functions provided by this framework, we can add, delete, modify, and query the contact information on the mobile phone through the development program interface.

AddressBookUI. framework

This framework uses AddressBook. framework, which directly contains four View Controller classes and the corresponding delegate protocol. these special view controllers provide the default user interface to operate the contact information on the mobile phone. Developers only need to create and display the instances of these view controllers.

The following AddressBook framework usage

The AddressBook framework consists of the following four groups of Apis:

ABAddressBook

ABAddressBook instances represent Address Book objects. This object provides a common programming interface that allows us to store contact information in the underlying database, access the information of these contacts in a transparent manner. in actual programming, it is always intended for ABAddressBook pointer (ABAddressBookRef) programming.

ABRecord

ABRecord indicates a common record object, which contains a large amount of common data (such as the last name, first name, phone number, and email of a contact ).

 

Each record has a unique ID in the underlying database. You can use the ABRecordGetRecordID () function to obtain the ID of the specified record.

You can use the ABRecordGetRecordType () function to obtain the record type.

If the record is a contact, the function returns the kABPersonType (0) Enumeration value;

This function returns the kABGroupType (1) enumerated value.

In actual programming, it is always intended for ABRecordRef programming.

ABPerson

ABPerson indicates the contact information. generally, "kABPersonType" ABRecordRef is used to indicate the contact information. the contact information stores the last name, name, address, email, and phone number of the contact. You can use a View Controller (such as ABPersonViewController) to display the contact information.

ABGroup

ABGroup indicates a group. Generally, "kABGroupType" ABRecordRef indicates a group. A contact can belong to multiple groups, and a group can contain multiple contacts.

The ABAddressBook framework is a process-oriented operation method. The four APIs of this framework provide a large number of functions.

 

 

Summary:

The ABAddressBook function is only responsible for managing the address book, including creating the address book, adding records to the address book, and deleting records from the address book. In addition, it also includes determining whether users are allowed to access the address book, saving or discarding modifications to the address book.

To add or delete a record to or from the address book, you must use ABRecordRef. The functions provided by ABRecord are mainly used to access a large number of attributes contained in ABRecord.

Each ABRecord consists of multiple attributes. The last name, name, phone number, and email of a contact belong to the attributes of ABRecord.

 

ABRecord provides the following common functions.

Bool ABRecordSetValue (ABRecordRef record, ABPropertyID property, CFTypeRef value, CFErrorRef * error)

Set the property attribute in the record to "value ". Property must be a constant defined by ABPerson and ABGroup. It is used to indicate preset attributes and different attribute values are required.

The contact name corresponds to the kABPersonFirstNameProperty constant. This type of attribute only requires a string value;

The contact's phone number corresponds to the kABPersonPhoneProperty constant. For this type of property, the value of ABMutableMultiValue is required.

CFTypeRef ABRecordCopyValue (ABRecordRef record, ABPropertyID property)

This function returns the value of the property in the record.

Bool ABRecordRemoveValue (ABRecordRef record, ABPropertyID property, CFErrorRef * error)

This function deletes the property value in the record, and returns whether the deletion is successful.

CFStringRef ABRecordCopyCompositeName (ABRecordRef record ):

This function returns compound name information (including surname \ Name \ organization and other information) in the record ).

ABRecordID ABRecordGetRecordID (ABRecordRef record)

This function is used to obtain the ID of the record.

ABRecordType ABRecordGetRecordType (ABRecordRef record)

This function is used to obtain the type of the record (contact record or group record)

Summary:

The ABRecord function is mainly used for the attributes contained in Operation Records. To create or obtain an existing record, you need to use the functions provided by ABPerson and ABGroup.

ABPerson provides a large number of ABRecord functions for obtaining "kABPersonType". These functions can create an empty ABRecord to add new records to the address book, you can also obtain the ABRecord that meets specific conditions from the address book, including the ABRecord obtained by ID and by name. The ABRecord obtained from the address book can be deleted or modified.

ABGroup provides a large number of ABRecord functions for obtaining "kABGroupType". The ABGroup function can create new ABRecord or load ABRecord from the database.

ABMutableMultiValue provides the following common functions:

ABMutableMultiValueRef ABMultiValueCreateMutable (ABPropertyType type ):

Create an ABMutableMultiValue that manages attribute values of the ABPropertyType

Bool ABMultiValueAddValueAndLabel (ABMutableMultiValueRef multiValue, CFTypeRef value, CFStringRef label, ABMultiValueIdentifier * outIdentifier ):

Add a property value to multiValue. value indicates the property value, and label indicates the label of the property value (such as the phone number, you can set "home", "work", and other labels)

 

Bool ABMultiValueReplaceValueAtIndex (ABMutableMultiValueRef multiValue, CFTypeRef value, CFIndex index)

Replace the attribute value at the index of multiValue with the new value.

Bool ABMultiValueReplaceLabelAtIndex (ABMutableMultiValueRef multiValue, CFStringRef label, CFIndex index)

Replace the attribute label at the index of multiValue with the new label.

Bool ABMultiInsertValueAndLabelAtIndex (ABMutableMultiValueRef multiValue, CFTypeRef value, = CFStringRef label, CFIndex index, ABMultiValueIdentifier * outIdentifier)

Insert a property value to the index of multiValue. value indicates the property value, and label indicates the label of the property value.

Bool ABMultiValueRemoveValueAndLabelAtIndex (ABMutableMultiValueRef multiValue, CFIndex index)

Delete the attribute value at the index of multiValue (delete both the attribute value and attribute value tag ).

Delete contact

It's easy to delete a contact, just a few steps

 

1

Create ABAddressBookRef, which gets a reference to the address book.

2

Obtain the ABRecordRef to be deleted, which gets a reference to the record to be deleted.

3

Call the ABAddressBookRemoveRecord () function to delete a specified record.

4

Call the ABAddressBookSave () function to save the delete operation to the underlying address book.

Code

@ Implementation LCViewController-(void) viewDidLoad {[super viewDidLoad];}-(IBAction) deletePerson :( id) sender {CFErrorRef error = nil; // create ABAddressBook, the 1st parameter of this function is not used currently. if NULL is passed in, you can use ABAddressBookRef AB = abaddressbookcreatewitexceptions (NULL, & error); if (! Error) {// request to access the user's address book ABAddressBookRequestAccessWithCompletion (AB, ^ (bool granted, CFErrorRef error) {// if the user permits access to the address book if (granted) {// obtain the record ABRecordRef rec = ABAddressBookGetPersonWithRecordID (AB, 3); BOOL result = ABAddressBookRemoveRecord (AB, rec, NULL); if (result) {// Save the changes made by the program to the address book. if the changes are successfully saved if (ABAddressBookSave (AB, NULL) {[self showAlert: @ "successfully deleted contacts with ID 3"];} else {[self showAlert: @ "error occurred when saving the modification"];} else {[self showAlert: @ "delete failed"] ;}}}) ;}- (void) showAlert :( NSString *) msg {// use UIAlertView to display msg information [[[UIAlertView alloc] initWithTitle: @ "prompt" message: msg delegate: nil cancelButtonTitle: @ "OK" otherButtonTitles: nil] show];}

 

 

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.