Any way to get the name of iPhone user?

后端 未结 5 1175
醉梦人生
醉梦人生 2020-12-28 17:46

Outside of asking the user to input their name, is there any way to get it off the device?

I tried this library, which attempts to extract the name from [UIDev

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-28 18:13

    You could use CloudKit. Following a snippet in Swift (ignoring errors):

    let container = CKContainer.defaultContainer()
    
    container.fetchUserRecordIDWithCompletionHandler(
        {
            (recordID, error) in
    
            container.requestApplicationPermission(
                .PermissionUserDiscoverability,
                {
                    (status, error2) in
    
                    if (status == CKApplicationPermissionStatus.Granted)
                    {
                        container.discoverUserInfoWithUserRecordID(
                            recordID,
                            completionHandler:
                            {
                                (info, error3) in
    
                                println("\(info.firstName) \(info.lastName)")
                            }
                        )
                    }
                }
            )
        }
    )
    

    The above code was based on the code at http://www.snip2code.com/Snippet/109633/CloudKit-User-Info

    to save folks time. in swift4:

        let container = CKContainer.default()
        container.fetchUserRecordID(
            completionHandler: {
                (recordID, error) in
                guard let recordID = recordID else {
                    return
                }
                container.requestApplicationPermission(
                    .userDiscoverability,
                    completionHandler: {
                        (status, error2) in
    
                        if (status == CKContainer_Application_PermissionStatus.granted)
                        {
                            if #available(iOS 10.0, *) {
                                container.discoverUserIdentity(withUserRecordID:
                                    recordID,
                                                               completionHandler:
                                    {
                                        (info, error3) in
                                        guard let info = info else {
                                            return
                                        }
                                        print("\(info.firstName) \(info.lastName)")
                                }
                                )
                            }
                        }
                }
                )
            }
        )
    

    however: CKUserIdentity no longer exposes either first or last name

    So this answer no longer works.

提交回复
热议问题