应用程序不是要求在iOS 9中访问联系人的联系人权限

时间:2016-06-03 13:18:19

标签: ios iphone contacts abaddressbook

我使用以下代码获取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");
    } 
}

我无法在此处找到问题,用户如何获得访问联系人的权限。任何建议都会有所帮助。

3 个答案:

答案 0 :(得分:3)

在iOS 9中不推荐使用ABAddressBookRequestAccessWithCompletion。现在您应该使用Contacts框架。这是目标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中),您可以做的是将用户重定向到设置。

答案 1 :(得分:2)

您需要使用ABAddressBookRequestAccessWithCompletion()

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

答案 2 :(得分:0)

如果您想检查用户指定的联系人权限,如果未获得权限,则显示提醒以移动用户进入设置以获得权限。

然后使用以下函数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,您可以使用“联系人”框架进行检查权限。