How can a user be logged into iCloud (iCloud Available), but still Not Authenticated?

I'm running Xcode 12.4, testing on iOS 13 and 14.

How can a user be logged in/iCloud Available, but still Not Authenticated?

When I fetch a record, the CKErrorCode is NotAuthenticated and it prints Couldn't get an authentication token

let predicate = NSPredicate(format: "id == %@", id)        
let query = CKQuery(recordType: RecordType.Media, predicate: predicate)
CKContainer.default().publicCloudDatabase.perform(query, inZoneWith: nil) { (ckRecords, error) in
    if let err = error {
        let code = err._code
        // error code is 9 - case NotAuthenticated /* Not authenticated (writing without being logged in, no user record) */
        return
    }

    // ...
}

But when I check to see if the user is logged in, I use the code below, and in both situations it prints iCloud Available

1-

if FileManager.default.ubiquityIdentityToken != nil {
    print("iCloud Available")
} else {
    print("iCloud Unavailable")
}

2-

CKContainer.default().accountStatus { (accountStatus, error) in
    switch accountStatus {
    case .available:
        print("iCloud Available")
    case .noAccount:
        print("No iCloud account")
    case .restricted:
        print("iCloud restricted")
    case .couldNotDetermine:
            print("Unable to determine iCloud status")
    @unknown default:
        print("Unable to determine iCloud status")
    }
}

FYI, this seems to happen in this order.

1- I'm home on wiFi, logged into iCloud, everything works fine.

2- I leave my house, switch to a hot spot, I'm still logged into iCloud and everything works fine

3- I come back into my house, switch back to wiFi, of course I'm still logged in, then the above issue occurs. It's as if it wants me to log in again even though I'm already logged in and it says iCloud Available.

This issue is happening on both the real device and the simulator.

UPDATE

I just found this post, it seems lots of devs are having this same problem

Answered by digitalglen in 703581022

Development and Production use entirely different containers, so records added to one don't exist in the other. So it's not like Development vs. Production is a simple attribute—it represents deep differences in configuration.

Apparently the issue is com.apple.developer.icloud-container-environment: Development vs Production.

Any of the iCloud posts that I made under Development aren't accessible while the Entitlement is set to Production. On the flip side any of the iCloud posts that I made under Production aren't accessible while the Entitlement is set to Entitlement.

I have no idea why it works this way but in short the NotAuthenticated issue occurs when trying to view a post that was made while in Development but the value is currently set to Production

Can anyone explain why it works this way?

Accepted Answer

Development and Production use entirely different containers, so records added to one don't exist in the other. So it's not like Development vs. Production is a simple attribute—it represents deep differences in configuration.

How can a user be logged into iCloud (iCloud Available), but still Not Authenticated?
 
 
Q