iOS应用不会显示对地址簿访问权限的警报

时间:2016-01-28 13:16:48

标签: ios addressbook

我正在开发一个需要显示联系人的应用。

它在Xcode 6.2上运行良好。但是当我在Xcode 7.2上运行这个项目时,它甚至都没有要求Permission警报。

我写了以下获取权限的方法。

-(void)requestAddressBookAccess
{
    ContactsViewController * __weak weakSelf = self;

    ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted, CFErrorRef error)
                                             {
                                                 if (granted)
                                                 {
                                                     dispatch_async(dispatch_get_main_queue(), ^{
                                                         [weakSelf accessGrantedForAddressBook];

                                                     });
                                                 }
                                             });
}

总是提供granted = falseerror = nil

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

在iOS 9中,您必须使用Contacts框架,因为地址簿框架已被删除

-(void)addressbookAuthorizationUsingContacts
{
    //#import #import <Contacts/Contacts.h>

   // Request authorization to Address Book Using Contacts
   CNContactStore *store = [[CNContactStore alloc] init];
   [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (granted == YES) {
        //keys with fetching properties
        NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];
        NSString *containerId = store.defaultContainerIdentifier;
        NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
        NSError *error;
        NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
        if (error) {
            NSLog(@"error fetching contacts %@", error);
        } else {
            NSString *phone;
            NSString *fullName;
            NSString *firstName;
            NSString *lastName;
            UIImage *profileImage;
            NSMutableArray *contactNumbersArray = [[NSMutableArray alloc]init];
            NSMutableArray *contactsArray = [[NSMutableArray alloc]init];
            for (CNContact *contact in cnContacts) {
                // copy data to my custom Contacts class.
                firstName = contact.givenName;
                lastName = contact.familyName;
                if (lastName == nil) {
                    fullName=[NSString stringWithFormat:@"%@",firstName];
                }else if (firstName == nil){
                    fullName=[NSString stringWithFormat:@"%@",lastName];
                }
                else{
                    fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
                }
                UIImage *image = [UIImage imageWithData:contact.imageData];
                if (image != nil) {
                    profileImage = image;
                }else{
                    profileImage = [UIImage imageNamed:@"person-icon.png"];
                }
                for (CNLabeledValue *label in contact.phoneNumbers) {
                    phone = [label.value stringValue];
                    if ([phone length] > 0) {
                        [contactNumbersArray addObject:phone];
                    }
                }
                NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers", nil];
                [contactsArray addObject:personDict];
                NSLog(@"The contactsArray are - %@",contactsArray);
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [tableViewContacts reloadData];
            });
        }
    }
}];
}

输出结果

The contactsArray are - (
    {
    fullName = "John Appleseed";
},
    {
    fullName = "Kate Bell";
},
    {
    fullName = "Anna Haro";
},
    {
    fullName = "Daniel Higgins";
},
    {
    fullName = "David Taylor";
},
    {
    fullName = "Hank Zakroff";
}
)

答案 1 :(得分:-1)

ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();

    if (status != kABAuthorizationStatusAuthorized && status != kABAuthorizationStatusNotDetermined) {

    // tell user to enable contacts in privacy settings

    NSLog(@"You previously denied access: You must enable access to contacts in settings");

    return;
}