GoZoner
GoZoner

Reputation: 70215

CKUserIdentity `nameComponents` is Empty

My App requires that the user has and allows iCloud access. With that access I expect to be able to access the users name. Yet, the following code produces an empty nameComponents result from CKUserIdentity

        var userParticipantIdentity: CKUserIdentity? = nil

        do {
            let userRecordID    = try await container.userRecordID()
            let userParticipant = try await container.shareParticipant (forUserRecordID: userRecordID)
            userParticipantIdentity = userParticipant.userIdentity
        }
        catch { ... throw ... }     

The resulting userParticipantIdentity.nameComponents returns a PersonNameComponents w/o any name values.

Given that my app requires iCloud access (and that various iOS 'discovery interfaces' have been deprecated), how can I go about getting a user's name?

More specifically, if you create a new Xcode project using CoreData w/ CloudKit and you then add the following to ContentView:

        .task {
            let container = controller.cloudKitContainer

            do { try controller.container.initializeCloudKitSchema() } catch {}

            do {
                let userStatus = try await container.accountStatus()
                guard case .available = userStatus
                else { print ("UserName: Error: No iCloud"); return }

                let userRecordID    = try await container.userRecordID()
                let userParticipant = try await container.shareParticipant(forUserRecordID: userRecordID)
                let userParticipantIdentity = userParticipant.userIdentity

                guard let userParticipantNameComponents = userParticipantIdentity.nameComponents
                else { print ("UserName: Error: No `nameComponents"); return }

                let userParticipantName = PersonNameComponentsFormatter().string(from: userParticipantNameComponents)
                print ("UserName: Got: `\(userParticipantName)`")
            }
            catch {
                print ("UserName: Error: \(error.localizedDescription)")
            }
        }

the runtime print out will be "UserName: Got: ``"

Upvotes: 1

Views: 26

Answers (0)

Related Questions