【发布时间】:2010-11-09 03:57:36
【问题描述】:
如果您打开 Settings -> General -> About,屏幕顶部会显示 Bob 的 iPhone。您如何以编程方式获取该名称?
【问题讨论】:
如果您打开 Settings -> General -> About,屏幕顶部会显示 Bob 的 iPhone。您如何以编程方式获取该名称?
【问题讨论】:
来自 UIDevice 类:
Swift 版本:
UIDevice.current.name
Objective-C 版本:
[[UIDevice currentDevice] name];
UIDevice 是一个提供 有关 iPhone 或 iPod 的信息 触摸设备。
提供的部分信息 UIDevice 是静态的,比如 device 名称或系统版本。
来源:http://servin.com/iphone/uidevice/iPhone-UIDevice.html
官方文档: Apple Developer Documentation > UIDevice Class Reference
【讨论】:
除了上面的答案,这是实际代码:
[[UIDevice currentDevice] name];
【讨论】:
以编程方式获取 iPhone 的设备名称
UIDevice *deviceInfo = [UIDevice currentDevice];
NSLog(@"Device name: %@", deviceInfo.name);
// Device name: my iPod
【讨论】:
记住:import UIKit
斯威夫特:
UIDevice.currentDevice().name
斯威夫特 3、4、5:
UIDevice.current.name
【讨论】:
这是 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;
【讨论】:
对于 xamarin 用户,请使用此
UIKit.UIDevice.CurrentDevice.Name
【讨论】:
在 Unity 中,使用 C#:
SystemInfo.deviceName;
【讨论】:
对于 Swift 4+ 版本,请使用以下代码:
UIDevice.current.name
【讨论】:
对于swift4.0及以上使用以下代码:
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
【讨论】: