Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the user "Name" using Swift?

Tags:

ios

swift

I'm trying to create an app that when the user open the first view it will provide with a nice greeting.

For example depending on the time of the day it would say "Good Morning/Afternoon/Night - User Name".

How do I get the name that the User has on his iPhone? I need the name that is on Settings > General > About _ NAME?

like image 869
Roger Avatar asked Feb 19 '15 02:02

Roger


2 Answers

Swift 2
UIDevice.currentDevice().name
Swift 3
UIDevice.current.name

like image 97
Dylan Avatar answered Nov 20 '22 18:11

Dylan


UIDevice.current.name gives back device name, so you should parse the string trying to distinguish between device type (iPhone, iPad and so on...) and the name. But is bit tricky: suppose You have "My iPhone" , "Mary's iPhone" or "IPhone of John" or similar.

Soem people try to use regular expressions, but result is not so good, specially for non-english names. see at:

http://stackoverflow.com/questions/8261961/better-way-to-get-the-users-name-from-device

like image 24
ingconti Avatar answered Nov 20 '22 20:11

ingconti