Any way to get the name of iPhone user?

后端 未结 5 1153
醉梦人生
醉梦人生 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:10

    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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-28 18:20

    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.

    0 讨论(0)
  • 2020-12-28 18:24

    For SWIFT you can use

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

    func NSUserName() -> String

    0 讨论(0)
  • 2020-12-28 18:29

    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.

    0 讨论(0)
提交回复
热议问题