显示当前用户的用户名(Swift)

时间:2014-09-25 18:42:30

标签: xcode swift

是否可以在macOS应用中显示当前用户的用户名?

3 个答案:

答案 0 :(得分:18)

您可以使用NSUserName()功能:

var username = NSUserName()!

答案 1 :(得分:1)

Swift 4.2

let userName = NSUserName()
let fullUserName = NSFullUserName()

答案 2 :(得分:0)

以下对我有用。它是从 this post 复制的。

CKContainer.default().requestApplicationPermission(.userDiscoverability) { (status, error) in
    CKContainer.default().fetchUserRecordID { (record, error) in
        CKContainer.default().discoverUserIdentity(withUserRecordID: record!, completionHandler: { (userID, error) in
            let fullName = (userID?.nameComponents?.givenName)! + " " + (userID?.nameComponents?.familyName)!
        })
    }
}