Roger
Roger

Reputation: 73

How to get the user "Name" using 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?

Upvotes: 7

Views: 8205

Answers (3)

ingconti
ingconti

Reputation: 11666

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

Upvotes: 3

Steffen
Steffen

Reputation: 161

let benutzerGeraeteName = UIDevice.current.name
        print("Hallo, \(benutzerGeraeteName)")

Upvotes: -1

Dylan
Dylan

Reputation: 1161

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

Upvotes: 2

Related Questions