5, Address book permissions

Keywords: iOS

5, Address book permissions

First, import the framework import < contacts / contacts. H > and configure the NSContactsUsageDescription field in info.plist.

  • Get current App authorization status
    Use the class method authorizationStatusForEntityType of CNContactStore class to get the authorization status. Pass in the parameter to CNEntityTypeContacts. The authorization status enumeration is:
typedef NS_ENUM(NSInteger, CNAuthorizationStatus)
{
    CNAuthorizationStatusNotDetermined = 0, // up in the air
    CNAuthorizationStatusRestricted, // Restricted
    CNAuthorizationStatusDenied, // refuse
    CNAuthorizationStatusAuthorized // allow
}
  • Action when no authorization is requested
    When the enumeration value of authorization status is CNAuthorizationStatusNotDetermined, the instance object of CNContactStore class can be used to call the instance method requestAccessForEntityType:completionHandler: to request authorization (that is, to display the authorization bullet box. At this time, it is better to hold the object for a long time to prevent the bullet box from being destroyed after exceeding the scope). The parameter is passed into CNEntityTypeContacts , block callback the authorization status selected by the user.

The above is the method after iOS9.0, and the following is the method before iOS9.0.

Before 9.0, you need to use the content of the ා import < addressbook / addressbook. H > framework, and use the ABAddressBookGetAuthorizationStatus() function to obtain the current authorization status, which is an enumeration type

typedef CF_ENUM(CFIndex, ABAuthorizationStatus) {
    // up in the air
    kABAuthorizationStatusNotDetermined = 0,    // deprecated, use CNAuthorizationStatusNotDetermined
    // Restricted
    kABAuthorizationStatusRestricted,           // deprecated, use CNAuthorizationStatusRestricted
    // refuse
    kABAuthorizationStatusDenied,               // deprecated, use CNAuthorizationStatusDenied
    // allow
    kABAuthorizationStatusAuthorized            // deprecated, use CNAuthorizationStatusAuthorized
}

The function ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {} is also used to request authorization when the enumeration value is kABAuthorizationStatusNotDetermined. The first parameter is ABAddressBookRef type, which is the return value of the function ABAddressBookCreate(), and the user authorization BOOL value is called back in the block.

The complete logic code is:

- (void)addressbook:(void(^)(BOOL allow))callback
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
    /**
     iOS 9 This framework is supported after use. Before use, you need to import the framework
     */
    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    if (status == CNAuthorizationStatusNotDetermined) // User did not decide
    {
        CNContactStore *store = [[CNContactStore alloc] init];
        /** Before requesting the user to authorize the use of the address book, you need to ensure that the following fields are configured in info.plist, otherwise crash will occur
         <key>NSContactsUsageDescription</key>
         <string>The user-defined prompt information can not be filled in < / String >
         */
        [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
            NSLog(@"[Mail list]Authorization status is:%@", granted ? @"allow" : @"Not allow");
            if (callback) {
                dispatch_async(dispatch_get_main_queue(), ^{//GCD is used to ensure that the UI is updated on the main thread
                    callback(granted);
                });
            }
        }];
    }
    else if (status == CNAuthorizationStatusRestricted || status == CNAuthorizationStatusDenied) // Restriction or prohibition
    {
        NSLog(@"[Mail list]Authorization status is:Limited or not allowed");
        if (callback) {
            callback(NO);
        }
    }
    else // allow
    {
        NSLog(@"[Mail list]Authorization status is:allow");
        if (callback) {
            callback(YES);
        }
    }
#else
    /**
     iOS 9 It seems that you need to add fields when using the address book. You need to import the corresponding "import < addressbook / addressbook. H > framework"
     */
    ABAddressBookRef ref = ABAddressBookCreate();
    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
    if (status != kABAuthorizationStatusAuthorized) { // When not allowed
        // Request user authorization by pop-up
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (error) {
                    NSLog(@"Error: %@", (__bridge NSError *)error);
                    if (callback) {
                        callback(NO);
                    }
                } else {
                    if (callback) {
                        callback(YES);
                    }
                }
            });
        });
    }else { // allow
        if (callback) {
            callback(YES);
        }
    }
#endif

Posted by rameshfaj on Thu, 02 Apr 2020 15:34:36 -0700