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?

Andrew
  • 15,357
  • 6
  • 66
  • 101
Andrew
  • 15,935
  • 28
  • 121
  • 203

5 Answers5

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.

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
rckoenes
  • 69,092
  • 8
  • 134
  • 166
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.

Community
  • 1
  • 1
Senseful
  • 86,719
  • 67
  • 308
  • 465
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.

Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
drowa
  • 682
  • 5
  • 13
  • 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. – algal Oct 22 '14 at 22:51
  • When I try this, I get the error message: Type CKApplicationsPermissions has no member 'PermissionUserDiscoverability'. – Tom Tallak Solbu Feb 07 '16 at 12:55
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

Community
  • 1
  • 1
Mutawe
  • 6,464
  • 3
  • 47
  • 90
-2

For SWIFT you can use

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

func NSUserName() -> String
TonkBerlin
  • 191
  • 1
  • 6
  • 1
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) 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.](//stackoverflow.com/help/deleted-answers) – Vickel Nov 11 '19 at 18:41
  • this returns `"mobile"` – Troy Dec 20 '21 at 12:07