IOS -- Address Book operation (for ios6)

Source: Internet
Author: User

A new project is about to begin. Because the prototype and UI are not yet in place, we have done some validation work in advance while there is a lot of free time. After that, we will immediately mark the project, address Book mentioned this time

Before ios5, you only need to add simple code to access the address book. The Code is as follows:

ABAddressBookRef addressBook = ABAddressBookCreate();

However, this is no longer the case for ios6. Since the previous IOS version was criticized for privacy, every app needs to be authorized to access the address book. (Of course, if the Chinese style is more boring, it prompts you to visit the calendar instead of the address book. Maybe foreigners think that the Chinese calendar is the address book)

The Code is as follows:

Abaddressbookref addressbook = nil; If ([uidevice currentdevice]. systemversion floatvalue]> = 6.0) {addressbook = abaddressbookcreatewitexceptions (null, null); // execute dispatch_semaphore_t Sema = dispatch_semaphore_create (0); then (addressbook, ^ (bool granted, cferrorref error) {encrypt (SEMA) ;}); encrypt (Sema, dispatch_time_forever); dispatch_release (SEMA);} else {addressbook = abaddressbookcreate ();}

The above code can be used to adapt to ios6 and ios5. However, the dialog box is displayed only when the first authorization request is sent, so we have to determine the authorization status on our own. Code:

NSLog(@"%ld",ABAddressBookGetAuthorizationStatus());

In this way, you can know the authorization status, and attach the official explanation:

// To check the app's access to contact data. Based upon the access, the app could// display or hide its UI elements that would access any AddressBook API.//// kABAuthorizationStatusNotDetermined// The user has not yet made a choice regarding whether this app can access the data class.//// kABAuthorizationStatusRestricted// This application is not authorized to access the data class. The user cannot change// this application’s status, possibly due to active restrictions such as parental controls// being in place.//// kABAuthorizationStatusDenied// The user explicitly denied access to the data class for this application.//// kABAuthorizationStatusAuthorized// This application is authorized to access the data class.//typedef CF_ENUM(CFIndex, ABAuthorizationStatus) {    kABAuthorizationStatusNotDetermined = 0,    kABAuthorizationStatusRestricted,    kABAuthorizationStatusDenied,    kABAuthorizationStatusAuthorized};AB_EXTERN ABAuthorizationStatus ABAddressBookGetAuthorizationStatus(void) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);

Once we know the authorization status, we can remind the user to change the authorization status.

After the program is installed, only the first time will prompt whether to access the address book, the second time will not be done, even if the program is deleted, it will not be reproduced, It is very inconvenient for everyone to debug, in fact, after the device is restored, it still appears.

Restore procedure: set-General-restore-Restore location and privacy

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.