如何在iOS 17 SwiftUI中获取用户名和姓氏

kq4fsx7k  于 2023-10-21  发布在  iOS
关注(0)|答案(2)|浏览(142)

我尝试从CloudKit检索当前用户的名字和姓氏。然而,我收到了一条来自Xcode的消息,表明“userDiscoverability”功能将在iOS 17.0中被弃用。此功能不再受支持,相反,我应该考虑使用“与其他iCloud用户共享CloudKit数据”功能。
下面是我一直在使用的函数:

func fetchUserName(completion: @escaping ( _ fullName:String, _ success:Bool) -> Void) {
        container.requestApplicationPermission(.userDiscoverability) { (status, error) in
            self.container.fetchUserRecordID { (record, error) in
                self.container.discoverUserIdentity(withUserRecordID: record!) { (userID, error) in
                    var name: String = ""
        
                    self.container.accountStatus { accountStatus, error in
                        if let error = error {
                            print(error.localizedDescription)
                        }
                        
                    }

                    guard let givenName = userID?.nameComponents?.givenName,
                          let familyName = userID?.nameComponents?.familyName else {
                        completion("", false)
                        print("Unable to fetch user name")
                        return
                    }
                    
                    let fullName: String = givenName + " " + familyName
                    name = fullName

                    DispatchQueue.main.async {
                        completion(name, true)
                    }
                }
            }
        }
    }

有人有办法解决这个问题吗?

b1uwtaje

b1uwtaje1#

您不再需要外部调用(container.requestApplicationPermission)。
为了检索名称,您需要首先获取活动用户记录ID,然后使用ID获取共享参与者,最后从CKShare.Participant对象检索名称。你的代码已经这样做了,它的修改器版本看起来像这样:

func fetchUserName(completion: @escaping ( _ fullName:String, _ success:Bool) -> Void) {
        self.container.fetchUserRecordID { (record, error) in
            self.container.fetchShareParticipant(withUserRecordID: record!) { (userID, error) in
                var name: String = ""
        
                self.container.accountStatus { accountStatus, error in
                    if let error = error {
                        print(error.localizedDescription)
                    }
                        
                }

                guard let givenName = userID?.nameComponents?.givenName,
                      let familyName = userID?.nameComponents?.familyName else {
                    completion("", false)
                    print("Unable to fetch user name")
                    return
                }
                    
                let fullName: String = givenName + " " + familyName
                name = fullName

                DispatchQueue.main.async {
                    completion(name, true)
                }
            }
        }
    }

如果尚未启用,则还需要enable CloudKit in your app

6yt4nkrj

6yt4nkrj2#

等待版本:

static func getUserInformation() async throws -> (id: String, name: String) {
    do {
        let recordID = try await container.userRecordID()
        let id = recordID.recordName
        
        let participant = try await container.shareParticipant(forUserRecordID: recordID)
        guard let nameComponents = participant.userIdentity.nameComponents else {
            throw CloudKitError.userIdentityUnknownName
        }
        let name = PersonNameComponentsFormatter().string(from: nameComponents)

        return (id, name)
    } catch {
        throw error
    }
}

相关问题