Any way to get the name of iPhone user?

走远了吗. 提交于 2019-11-29 02:27:28

问题


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 [UIDevice currentDevice] name], but that doesn't work in a lot of situations:

https://github.com/tiboll/TLLNameFromDevice

Is the user's name present in the phonebook or anywhere else that we have access to in iOS 6?


回答1:


Well you could go through all the contacts in the AddressBook and see if any of them are marked with the owner flag.

Just be aware that doing this will popup the "this app wants access to the address book" message. Also Apple isn't very keen on these kind of things. In the app review guide it is specified that an app can not use personal information without the user's permission.




回答2:


You could use Square's solution:

  1. Get the device's name (e.g. "John Smith's iPhone").
  2. Go through the contacts on the phone and look for a contact named "John Smith".

JBDeviceOwner and ABGetMe will both do this for you.




回答3:


You can use:

NSLog(@"user == %@",[[[NSHost currentHost] names] objectAtIndex:0]);

I did receive compiler warnings that the methods +currentHost and -names were not found. Given the warning, I’m not sure of Apple’s intention to make this available (or not) as a publicly accessible API, however, everything seemed to work as expected without the need to include any additional header files or linking in additional libraries/frameworks.

Edit 1: You may also take a look at this Link

Edit 2: If you have integrated your app with Facebook you can easily retrieve the user info, see Facebook Fetch User Data




回答4:


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.




回答5:


For SWIFT you can use

NSUserName() returns the logon name of the current user.

func NSUserName() -> String


来源:https://stackoverflow.com/questions/16897605/any-way-to-get-the-name-of-iphone-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!