Any way to get the name of iPhone user?
Asked Answered
K

5

17

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?

Kyle answered 3/6, 2013 at 13:14 Comment(1)
I don't think it's possible on iOS: https://mcmap.net/q/745064/-ios-mfmailcomposeviewcontroller-and-getting-users-name-from-contact-cardZoophyte
E
12

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.

Eddins answered 3/6, 2013 at 13:19 Comment(3)
Finally an answer that doesn't immediately repeat the lazy "it's impossible" non-answer... (+1)Theona
Where is this owner flag?Withers
@rckoenes, please elaborate on this 'owner' flag. Some example code would be very helpful. Thanks.Nejd
N
9

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.

Norbert answered 24/1, 2014 at 22:26 Comment(0)
D
3

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.

Diegodiehard answered 22/10, 2014 at 22:10 Comment(2)
It should be noted that, if this permission has not already been granted, this will prompt the user for permission to let himself be discoverable by others users of the same app who have his email address in their address book.Vaccinate
When I try this, I get the error message: Type CKApplicationsPermissions has no member 'PermissionUserDiscoverability'.Eggshell
C
2

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

Chester answered 3/6, 2013 at 13:28 Comment(0)
R
-2

For SWIFT you can use

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

func NSUserName() -> String
Ribera answered 11/11, 2019 at 18:11 Comment(2)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.Eliathas
this returns "mobile"Indigent

© 2022 - 2024 — McMap. All rights reserved.