如何确定当前的iPhone/设备型号?

542

有没有一种方法可以在Swift中获取设备型号名称(iPhone 4S,iPhone 5,iPhone 5S等)?

我知道有一个名为UIDevice.currentDevice().model的属性,但它只返回设备类型(iPod touch,iPhone,iPad,iPhone Simulator等)。

我也知道可以使用以下方法轻松地在Objective-C中完成:

#import <sys/utsname.h>

struct utsname systemInfo;
uname(&systemInfo);

NSString* deviceModel = [NSString stringWithCString:systemInfo.machine
                          encoding:NSUTF8StringEncoding];

但我正在使用Swift开发我的iPhone应用,所以有人可以帮我找到在Swift中解决这个问题的等效方法吗?


5
用 Objective C 编写代码,然后在 Swift 中调用它。 - Kevin
1
请看这里 https://dev59.com/h1YO5IYBdhLWcg3wMO1y#52821290 - ale_stro
@jww CarPlay 运行在 iOS 设备上。iOS 只使用汽车(音响)的显示器、触摸输入、扬声器等,所有操作都在 iOS 设备上执行,而不是在汽车(音响)上执行。 - d4Rk
Xamarin iOS: https://github.com/dannycabrera/Get-iOS-Model - Nick Kovalsky
有没有简单的方法可以获得它? - flymaxelib
显示剩余2条评论
32个回答

-2
struct DeviceType {
        static let IS_IPHONE_4_OR_LESS = UIDevice.current.userInterfaceIdiom == .phone && Constants.SCREEN_MAX_LENGTH < 568
        static let IS_IPHONE_5 = UIDevice.current.userInterfaceIdiom == .phone && Constants.SCREEN_MAX_LENGTH == 568
        static let IS_IPHONE_6 = UIDevice.current.userInterfaceIdiom == .phone && Constants.SCREEN_MAX_LENGTH == 667
        static let IS_IPHONE_6P = UIDevice.current.userInterfaceIdiom == .phone && Constants.SCREEN_MAX_LENGTH == 736
        static let IS_IPAD = UIDevice.current.userInterfaceIdiom == .pad && Constants.SCREEN_MAX_LENGTH == 1024
    }

-2
在Swift 3中,它将是这样的。
 UIDevice.current.model

3
无论我在哪个模拟器上运行,得到的结果都只是“iPhone”。 - William T.
1
这个解决方案只返回模型为“iPhone”或“iPod touch”,而不是像“iPhone 6s”这样的特定设备。 - user4886069

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接