IOS-Address Book, ios Address Book Development

Source: Internet
Author: User

IOS-Address Book, ios Address Book Development
1. Overview:

* For every mobile device, there is a built-in database ----- address book.

* On IOS, the address book is stored in the SQLite3 database.

* Because different applications cannot access the database directly, you must use the APIS opened by Apple to access the database.

* Apple's APIs for data access are AddressBook and AddressBookUI ).

* For security reasons, the access to the address book must be authorized by the user only once.

2. Differences between AddressBook and AddressBookUI:

* AddressBook is a low-level API that can obtain data in the address book, but it requires a self-built UI interface.

* AddressBookUI is an advanced API that provides a UI for us and is easy to use.

3. Detailed description of AddressBook

* In AddressBook, the commonly used classes are ABAddressBook, ABPerson, ABGroup, and ABRecord.

* ABAddressBook: encapsulates the address book interface. (Corresponding to ABAddressBookRef)

* ABPerson: encapsulate the personal information data of a contact. (Corresponding to ABPersonRef)

* ABGroup: encapsulates the address book group information data. (Corresponding to ABGroupRef)

* ABRecord: encapsulates a record of the database. (Corresponding to ABRecordRef)

To obtain the address book information, first import the AddressBook framework and write the header file <AddressBook/AddressBook. h>

To access the address book, the first step is to create an address book object for the user to request access to the address book. The Code is as follows:

1 ABAddressBookRef addressbook = ABAddressBookCreateWithOptions(NULL, NULL);

* If this is the first access to the address book, you need to obtain the user's authorization. This is not the first access. The system provides the following functions:

* ABAddressBookRequestAccessWithCompletion (<# ABAddressBookRef addressBook #>, <# ^ (bool granted, CFErrorRef error) completion #>)

* If you click Allow for the first access, the value of granted is one.

* Access to the address book is allowed only when the granted value is true.

* The code for getting all records in the address book is as follows:

1 NSArray *array = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressbook);

* Next we will get the name of the contact in the address book: the code is as follows:

1 ABRecordRef record = array[i];2 NSString *name = (NSString *)ABRecordCopyCompositeName(record);

 * When obtaining the mobile phone number of a contact, it belongs to a multi-value attribute, which is a little different from the subsequent contact name. The contact name belongs to the single-Value Attribute. The Code is as follows:

1 ABMultiValueRef ref = ABRecordCopyValue(record, kABPersonPhoneProperty);2 NSString *phone = ABMultiValueCopyValueAtIndex(ref, 0);

* The overall code is: (the function of the second function is to remove the special characters in the phone number, because the numbers we get are separated by Apple)

-(void)getAddressbookContent{    ABAddressBookRef addressbook = ABAddressBookCreateWithOptions(NULL, NULL);    ABAddressBookRequestAccessWithCompletion(addressbook, ^(bool granted, CFErrorRef error) {        if (granted == YES)        {            NSArray *array = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressbook);            for (int i = 0; i < array.count; i ++)            {                ABRecordRef record = array[i];                NSString *name = (NSString *)ABRecordCopyCompositeName(record);                ABMultiValueRef ref = ABRecordCopyValue(record, kABPersonPhoneProperty);                NSString *phone = ABMultiValueCopyValueAtIndex(ref, 0);                NSString *phone1 = phone;                phone1 = [self deleteSpecialString:phone1];                contact *c = [[contact alloc]initWithName:name withPhone:phone1];                [_arrayContact addObject:c];                [c release];                CFRelease(name);                CFRelease(phone);                CFRelease(ref);            }        }    });}-(NSString *)deleteSpecialString:(NSString *)string{    string = [string stringByReplacingOccurrencesOfString:@"(" withString:@""];    string = [string stringByReplacingOccurrencesOfString:@")" withString:@""];    string = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];    return string;}
4. Brief description of ABAddressBookUI

* Using this class, we can call up the address book interface directly. The Code is as follows:

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];[self presentViewController:picker animated:YES completion:nil];

This class has several common proxy methods:

Click Cancel.

-(Void) verify whether ickernavigationcontrollerdidcancel :( ABPeoplePickerNavigationController *) Then restart icker

Click the contact information to go to the details page.

-(BOOL) warn about ickernavigationcontroller :( ABPeoplePickerNavigationController *) warn about icker shouldContinueAfterSelectingPerson :( ABRecordRef) person

Click other information on the contact details page to see if you want to go to another page (for example, click time to open the calendar)

-(BOOL) warn about ickernavigationcontroller :( ABPeoplePickerNavigationController *) warn about icker shouldContinueAfterSelectingPerson :( ABRecordRef) person property :( ABPropertyID) property identifier :( ABMultiValueIdentifier) identier

 

5. The above introduction is only the basic use of the address book. Of course, the address book method is more than this. For example, you can modify, add, and delete contact information. Here, we mainly introduce how to obtain the address book information.

 

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.