应用程序未请求联系人权限以访问iOS 9中的联系人



我正在使用以下代码来获取iPhone联系人,但我的应用程序未获得iOS 9中允许联系人的权限。我从堆栈中找到了这段代码,其他引用也是一样的.

   - (void)getPersonOutOfAddressBook
{
    //1
    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    if (addressBook != nil) {
        NSLog(@"Succesful.");
        //2
        NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
        //3
        NSUInteger i = 0; for (i = 0; i < [allContacts count]; i++)
        {
            NSMutableDictionary *persiondict  =[[NSMutableDictionary alloc]init] ;
//            Person *person = [[Person alloc] init];
            ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
            //4
            NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson,
                                                                                  kABPersonFirstNameProperty);
            NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
            NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
//            person.firstName = firstName;
//            person.lastName = lastName;
//            person.fullName = fullName;
            [persiondict setValue:fullName  forKey:@"fullName"] ;
            //email
            //5
            ABMultiValueRef emails = ABRecordCopyValue(contactPerson, kABPersonEmailProperty);
            //6
            NSUInteger j = 0;
            for (j = 0; j < ABMultiValueGetCount(emails); j++) {
                NSString *email = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, j);
                if (j == 0) {
//                    person.homeEmail = email;
                    [persiondict setValue:email  forKey:@"email"] ;
//                    NSLog(@"person.homeEmail = %@ ", person.homeEmail);
                }
                else if (j==1)
                [persiondict setValue:email  forKey:@"email"] ;
            }
            //7 
            [ArrUserOfContacts addObject:persiondict];
        }
        //8
        CFRelease(addressBook);
    } else { 
        //9
        NSLog(@"Error reading Address Book");
    } 
}

我在这里找不到问题,用户怎么能获得访问联系人的权限。任何建议都会有所帮助。

ABAddressBookRequestAccessWithCompletion在iOS 9中被弃用。现在您应该使用联系人框架。这是目标 C 中的一个示例:

CNContactStore * contactStore = [CNContactStore new];
[contactStore requestAccessForEntityType:entityType completionHandler:^(BOOL granted, NSError * _Nullable error) {
             if(granted){
                 //
             }
         }];

在 Swift 3 中:

CNContactStore().requestAccess(for: .contacts, completionHandler: { granted, error in
        if (granted){
             //
        }
    })

仅当用户未拒绝或批准应用中联系人的权限时,才会请求权限。您不能请求已被用户拒绝的权限(至少现在在iOS 10中),您可以做的是将用户重定向到"设置"。

您需要使用 ABAddressBookRequestAccessWithCompletion() 请求权限

ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
  if (!granted){
    NSLog(@"Just denied");
    return; 
  }
  NSLog(@"Just authorized");
});

如果要检查用户是否授予联系人权限,如果未授予权限,则在设置中显示移动用户以授予权限的警报。

然后使用以下函数checkContactsPermission为:

-(void)checkContactsPermission {
    //Check permission status
    switch (ABAddressBookGetAuthorizationStatus()) {
        case kABAuthorizationStatusAuthorized:
            // Already permission given
            break;
        case kABAuthorizationStatusDenied:{
            // Permission not given so move user in settings page to app.
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert!" message:@"his app requires access to your contacts." preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction* SettingsButton = [UIAlertAction actionWithTitle:@"Settings"
                                                                     style:UIAlertActionStyleDefault
                                                                   handler:^(UIAlertAction * action)
                                             {
                                                 NSURL * settingsURL = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@",UIApplicationOpenSettingsURLString,[[NSBundle mainBundle]bundleIdentifier]]];
                                                 if (settingsURL) {
                                                     [[UIApplication sharedApplication] openURL:settingsURL];
                                                 }
                                             }];
            UIAlertAction* DeniedButton = [UIAlertAction actionWithTitle:@"Denied"
                                                                   style:UIAlertActionStyleDefault
                                                                 handler:^(UIAlertAction * action)
                                           {
                                           }];
            [alert addAction:SettingsButton];
            [alert addAction:DeniedButton];
            [self presentViewController:alert animated:YES completion:nil];
        }
        case kABAuthorizationStatusRestricted: {
            // Permission not given so move user in settings page to app.
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert!" message:@"his app requires access to your contacts." preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction* SettingsButton = [UIAlertAction actionWithTitle:@"Settings"
                                                                style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action)
                                        {
                                            NSURL * settingsURL = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@",UIApplicationOpenSettingsURLString,[[NSBundle mainBundle]bundleIdentifier]]];
                                            if (settingsURL) {
                                                [[UIApplication sharedApplication] openURL:settingsURL];
                                            }
                                        }];
            UIAlertAction* DeniedButton = [UIAlertAction actionWithTitle:@"Denied"
                                                               style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * action)
                                       {
                                       }];
            [alert addAction:SettingsButton];
            [alert addAction:DeniedButton];
            [self presentViewController:alert animated:YES completion:nil];
        }
            break;
        case kABAuthorizationStatusNotDetermined:
             // Permission not determin. so request for permission.
            ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
                if (granted){
                    // Already permission given
                }
            });
            break;
    }
}

对于iOS 10,您可以使用联系人框架进行检查权限。

最新更新