0

次のcpdeがあります:

ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, ABRecordGetRecordID(self.recordRef_));

 CFErrorRef  error = NULL;

    if ([self.nameTextField_.text isNotNull]){
        NSArray *nameStringArray = [self.nameTextField_.text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)([nameStringArray objectAtIndex:0]), NULL);
        if ([nameStringArray count] > 1){
            ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)([nameStringArray lastObject]), &error);
        }
    }

ただし、このコードの後、次のエラーが表示されます。

Error Domain=ABAddressBookErrorDomain Code=0 "The operation couldn’t be completed. (ABAddressBookErrorDomain error 0.)"

これはなぜですか?

4

2 に答える 2

2

エラー メッセージはあまり役に立たないように見えますが、コードの残りの部分は有効であるように見えるため、アプリケーションがアドレス帳データベースへのアクセスをユーザーに許可されていないと推測されます。次のようなコードを使用して、アドレス帳にアクセスする前に認証ステータスを確認します。

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
    // We're good to go
} else {
    // Hasn't been authorized by the user
    // You can check the exact authorization status for more information on the
    // exact reason why you can't access the Address Book (e.g. denied, restricted, etc.)
}
于 2013-02-11T00:38:33.687 に答える