Getting the iPhone User Name
April 16, 2009
The original tip I wrote (shown below) does retrieve the user name, however, giving its undocumented status as well as the returning a string with punctuation removed, the approach suggested by Pascal in the comment section is much better:
[[UIDevice currentDevice] name];
When running this code from my device it returns "John’s iPhone" and when running from the simulator, it returns "iPhone Simulator." Much better. Thanks Pascal.
[Original post follows...]
Here’s a few lines of code to get the name of the owner tied to an iPhone:
NSArray *nameArray = [[NSHost currentHost] names]; NSString *user = [nameArray objectAtIndex:0];
You will need to do a little additional formatting of the NSString if you need to display the returned value, as the result replaces spaces and punctuation – "John’s iPhone" is returned as "John-s-iPhone". When I ran this on the simulator, the output I received was : john-3.local.
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.