Whenever a user in my app first accesses the app, this code below checks to see if the app is authorized to get the contacts from the device.

If the user needs to allow it, and the user accepts, the app crashes with a SIGSEGV error. The next time the user logs in, it works just fine and all the contacts are present (the authorize popup doesn't come up).

Any ideas from this method, what could be causing it?

- (void)getPersonOutOfAddressBook
{
    CFErrorRef error = NULL;

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    if (addressBook != nil){
        NSLog(@"getPersonOutOfAddressBook - addressBook successfull!.");


        if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
            ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
                if(granted){
                    [self addContactsIntoTableData:addressBook];
                }
            });
        }else if(ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
            [self addContactsIntoTableData:addressBook];
        }
    }

    CFRelease(addressBook);
    [self.tableView reloadData];
}

Error

Thread 6: EXC_BAD_ACCESS(code=2,address 0x0)

at this line

 [self addContactsIntoTableData:addressBook];

which it seems that there is some issues with the addressBook reference at this point...

有帮助吗?

解决方案

When you first create the address book, if you end up in the completion handler after requesting access, you need to update your address book reference.

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (granted && !error) {
                ABAddressBookRevert(addressBook);
                [self addContactsIntoTableData:addressBook];
            }
        });
    });
} else if(ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized){
    [self addContactsIntoTableData:addressBook];
}

Also keep in mind that you are calling reloadData long before the user allows access to the contacts.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top