[Ios] ios6 ios7 access and use the system address book

Source: Internet
Author: User

[Ios] ios6 ios7 access and use the system address book

Import AddressBook and AddressBookUI framework

Before iOS 6, you can use the following method to obtain the address book

ABAddressBookRef addressBook = ABAddressBookCreate ();

However, after iOS 6, this method is discarded. You can use the following method to obtain the address book.
AB _EXTERN ABAddressBookRef abaddressbookcreatewitexceptions (CFDictionaryRef options, CFErrorRef * error) _ OSX_AVAILABLE_STARTING (_ MAC_NA ,__ IPHONE_6_0 );

In addition, after ios6, each App should be authorized to access the address book:
// System-defined block
Typedef void (^ ABAddressBookRequestAccessCompletionHandler) (bool granted, CFErrorRef error );
// Obtain the authorization method. The second parameter is the block defined above.
AB _EXTERN void ABAddressBookRequestAccessWithCompletion (ABAddressBookRef addressBook, ABAddressBookRequestAccessCompletionHandler completion) _ OSX_AVAILABLE_STARTING (_ MAC_NA ,__ IPHONE_6_0 );
The Code is as follows:


-(Void) getAddress {// typedef CFTypeRef ABAddressBookRef; // typedef const void * CFTypeRef; // pointer to the constant ABAddressBookRef addressBook = nil; // determine the current system version if ([[UIDevice currentDevice]. systemVersion floatValue]> = 6.0) {// if not less than 6.0, use the corresponding api to obtain the address book. Note that you must first request the user's consent, the content of this address book is blank addressBook = abaddressbookcreatewitexceptions (NULL, NULL); // execute downward after waiting for consent // to ensure user consent before the operation, the multi-thread semaphore mechanism is used at this time, create a semaphore. The number of semaphores 0 indicates no resources. Dispatch_semaphore_wait will wait immediately. If you do not understand this, see The GCD semaphore synchronization. Dispatch_semaphore_t sema = dispatch_semaphore_create (0); // send an access Address Book request ABAddressBookRequestAccessWithCompletion (addressBook, ^ (bool granted, CFErrorRef error) {// if the user agrees, will execute the method in this block // This method sends a signal, add a number of resources dispatch_semaphore_signal (sema) ;}); // if the previous block is not executed, when the number of sema resources is zero, the program will be blocked // when the user chooses to agree, the block method will be executed, and the number of sema resources will be 1; dispatch_semaphore_wait (sema, DISPATCH_TIME_FOREVER ); dispatch_release (sema);} // if the system is earlier than 6.0, directly access else {addressBook = ABAddressBookCreate ();} // The Address Book information has been obtained without consent, start to retrieve CFArrayRef results = ABAddressBookCopyArrayOfAllPeople (addressBook); // number of contact entries (use long instead of int for 64-bit compatibility) long peopleCount = CFArrayGetCount (results ); for (int I = 0; I
 
  


When you request an access address book, an alert box is displayed for the first time. You can use the following methods to determine the status to remind users
Typedef CF_ENUM (CFIndex, ABAuthorizationStatus) {latency = 0, latency, latency}; AB _EXTERN ABAuthorizationStatus values (void) _ OSX_AVAILABLE_STARTING (_ MAC_NA ,__ IPHONE_6_0 );
// Obtain the status, which can be determined based on the Status
ABAuthorizationStatus statu = ABAddressBookGetAuthorizationStatus ();


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.