我正在使用以下代码获取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()的请求权限

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

关于ios - 应用程序未要求联系人权限以访问iOS 9中的联系人,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37615482/

10-14 21:54