How do you get an iPhone's device name
Asked Answered
A

12

173

If you open Settings -> General -> About, it'll say Bob's iPhone at the top of the screen. How do you programmatically grab that name?

Assessor answered 8/7, 2009 at 19:38 Comment(0)
H
223

From the UIDevice class:

Swift version:

UIDevice.current.name

Objective-C version:

[[UIDevice currentDevice] name];

The UIDevice is a class that provides information about the iPhone or iPod Touch device.

Some of the information provided by UIDevice is static, such as device name or system version.

source: http://servin.com/iphone/uidevice/iPhone-UIDevice.html

Offical Documentation: Apple Developer Documentation > UIDevice Class Reference

Hinterland answered 8/7, 2009 at 19:41 Comment(3)
Be careful: the tutorial at that link, while quite useful, is aimed at OS 2.2, and uses some methods that are deprecated in 3.0.Showroom
@Tim: You are absolutely right. I didn't think of that. Though, I wasn't suggesting the tutorial; I was simply providing my source of information plus a source for more information.Hinterland
@FrankV What permissions should I request from the user in order to get myMusicAppName to change his Iphone name? How do I do that in Swift? Thank youAcuminate
F
182

In addition to the above answer, this is the actual code:

[[UIDevice currentDevice] name];

Frostbitten answered 27/10, 2011 at 16:19 Comment(0)
S
110

Remember: import UIKit

Swift:

UIDevice.currentDevice().name

Swift 3, 4, 5:

UIDevice.current.name
Scrunch answered 17/1, 2015 at 15:31 Comment(4)
Must have import UIKitDuffel
swift 5.1: UIDevice.current.nameDusty
Yes, UIDevice.current.name worked for me in Swift 5. Similarly I can tap into .model .batteryLevel etc Thanks!Anchorage
UIDevice.current.name its not work under iOS16 (beta3)Resurrection
G
15

Here is class structure of UIDevice

+ (UIDevice *)currentDevice;

@property(nonatomic,readonly,strong) NSString    *name;              // e.g. "My iPhone"
@property(nonatomic,readonly,strong) NSString    *model;             // e.g. @"iPhone", @"iPod touch"
@property(nonatomic,readonly,strong) NSString    *localizedModel;    // localized version of model
@property(nonatomic,readonly,strong) NSString    *systemName;        // e.g. @"iOS"
@property(nonatomic,readonly,strong) NSString    *systemVersion;
Gaiser answered 21/6, 2016 at 11:12 Comment(0)
N
15

Unfortunately from iOS 16 onwards you need a special entitlement. You can request it here: https://developer.apple.com/contact/request/user-assigned-device-name/ i

Nanna answered 14/1, 2023 at 23:0 Comment(0)
P
10

For swift4.0 and above used below code:

    let udid = UIDevice.current.identifierForVendor?.uuidString
    let name = UIDevice.current.name
    let version = UIDevice.current.systemVersion
    let modelName = UIDevice.current.model
    let osName = UIDevice.current.systemName
    let localized = UIDevice.current.localizedModel
    
    print(udid ?? "") // ABCDEF01-0123-ABCD-0123-ABCDEF012345
    print(name)       // Name's iPhone
    print(version)    // 14.5
    print(modelName)  // iPhone
    print(osName)     // iOS
    print(localized)  // iPhone
Poleyn answered 13/10, 2020 at 4:18 Comment(2)
this is great. so helful.Wenda
UIDevice.current.name its not work under iOS16 (beta3), Apple changed again.Resurrection
D
6

For xamarin user, use this

UIKit.UIDevice.CurrentDevice.Name
Dashtilut answered 6/10, 2016 at 11:44 Comment(1)
Thank you. For Swift 3 : UIDevice.current.nameBerkey
N
6

For Swift 4+ versions, please use the below code:

UIDevice.current.name
Nannienanning answered 19/6, 2019 at 14:3 Comment(0)
O
4

To get an iPhone's device name programmatically

 UIDevice *deviceInfo = [UIDevice currentDevice];

 NSLog(@"Device name:  %@", deviceInfo.name); 

// Device name: my iPod

Outgoing answered 25/9, 2014 at 7:18 Comment(0)
R
4

Device Name would not work out of Box now, Need to have a special entitlement.

Entitlement : com.apple.developer.device-information.user-assigned-device-name

Doc : https://developer.apple.com/forums/thread/708275

Official : https://developer.apple.com/documentation/uikit/uidevice/1620015-name

Robenarobenia answered 15/9, 2022 at 8:22 Comment(0)
C
1

In Unity, using C#:

SystemInfo.deviceName;
Caduceus answered 4/2, 2019 at 3:4 Comment(0)
P
0

As of iOS 16 name cannot be used for that and unfortunately I couldn't find an alternative. From UIKit.UIDevice:

open var name: String { get } // Synonym for model. Prior to iOS 16, user-assigned device name (e.g. @"My iPhone").
Pandurate answered 3/11, 2022 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.