2

According to the documentation with iOS6 an address book should be created using ABAddressBookCreateWithOptions.

It also says if the caller does not have access to the db then this method will return null.

However access is requested by calling ABAddressBookRequestAccessWithCompletion, which takes an ABAddressBookRef as a parameter.

So according to the documentation you can't get an ABAddressBookRef in the first place if you don't have access, but to get access you have to have an ABAddressBookRef to pass as a parameter.

Eh. Catch 22? How do you create an ABAddressBookRef then?

Been googling for some example/tutorial code for this but haven't found any.

TIA

4

2 回答 2

4

I use code like this:

if (ABAddressBookCreateWithOptions) {
    _book = ABAddressBookCreateWithOptions(NULL, NULL);
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(_book, ^(bool granted, CFErrorRef error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (granted && !error) {
                    ABAddressBookRevert(_book);
                }
            });
        });
    }
} else {
    _book = ABAddressBookCreate();
}

where _book is my ABAddressBookRef.

If my app has been denied access then _book will be NULL and you can't access the address book.

于 2012-10-24T17:34:28.077 回答
0

See my answer here:

https://stackoverflow.com/a/13671852/341994

ABAddressBookRequestAccessWithCompletion is useless and should be ignored.

  • If you need to know the authorization status, call ABAddressBookGetAuthorizationStatus.

  • If the authorization is undetermined (the user has never run this app), just attempt to access the database. This will cause the authorization alert to appear.

  • If the user has authorized or denied access, ABAddressBookRequestAccessWithCompletion does nothing of any value so there's no point calling it.

于 2012-12-02T17:41:37.683 回答