Search code examples
macoscloudkit

Fetching user record ID with CloudKit fails


I'm trying to find the user record ID with CloudKit. I've verified that the account status is CKAccountStatusAvailable. Then I try this:

[[CKContainer defaultContainer] fetchUserRecordIDWithCompletionHandler:^(CKRecordID *recordID, NSError *error) {
    if (error == nil) {
        NSLog(@"Got user record ID (no permission req): %@", recordID);
    } else {
        NSLog(@"Couldn't get user record ID, error=%@", error);
    }
}];

This inevitably fails with the following result:

(lldb) po error
<CKError 0x608000043990: "Not Authenticated" (9/1004); "Account couldn't get container scoped user id, no underlying error received">
(lldb) po [error userInfo]
{
    NSDebugDescription = "CKInternalErrorDomain: 1004";
    NSLocalizedDescription = "Account couldn't get container scoped user id, no underlying error received";
    NSUnderlyingError = "<CKError 0x608000044080: \"Unknown Error\" (1004)>";
}

The "not authenticated" bit seems to be incorrect-- I'm logged in to an iCloud account and other iCloud apps (e.g. TextEdit, Reminders) work normally (and anyway if I weren't, I shouldn't be getting CKAccountStatusAvailable). What else can/should I be doing here?

This is on OSX, Yosemite beta 3. Update: unchanged in beta 4.


Solution

  • The problem was confusion of default vs. custom CloudKit containers. I had configured my app to use a custom container, but was attempting to use the default container in my code (I was probably a little confused when setting up the capabilities). Anyway the key was to get the container name from the app's capabilities and then look it up at run time using [CKContainer containerWithIdentifier:] instead of using [CKContainer defaultContainer].

    The error message was unfortunately not helpful, and I'm surprised that [CKContainer defaultContainer] did not return nil, but in any case the code works now.