Any way to get the name of iPhone user?

南楼画角 提交于 2019-11-30 04:55:51
rckoenes

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.

Senseful

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.

Mutawe

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

drowa

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.

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