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

本文翻译自:How to determine the current iPhone/device model?

Is there a way to get the device model name (iPhone 4S, iPhone 5, iPhone 5S, etc) in Swift? 有没有办法在Swift中获取设备型号名称(iPhone 4S,iPhone 5,iPhone 5S等)?

I know there is a property named UIDevice.currentDevice().model but it only returns device type (iPod touch, iPhone, iPad, iPhone Simulator, etc). 我知道有一个名为UIDevice.currentDevice().model的属性,但它仅返回设备类型(iPod touch,iPhone,iPad,iPhone Simulator等)。

I also know it can be done easily in Objective-C with this method: 我也知道可以使用以下方法在Objective-C中轻松完成此操作:

#import <sys/utsname.h>

struct utsname systemInfo;
uname(&systemInfo);

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

But I'm developing my iPhone app in Swift so could someone please help me with the equivalent way to solve this in Swift? 但是我正在用Swift开发我的iPhone应用程序,所以有人可以帮我用等效的方法来解决这个问题吗?


#1楼

参考:https://stackoom.com/question/1lDJG/如何确定当前的iPhone-设备型号


#2楼

Dealing with c structs is painful in swift. 快速处理结构。 Especially if they have some kind of c arrays in it. 特别是如果它们中包含某种c数组。 Here is my solution: Continue to use objective-c. 这是我的解决方案:继续使用Objective-C。 Just create a wrapper objective-c class that does this job and then use that class in swift. 只需创建一个完成此任务的包装Objective-C类,然后迅速使用该类即可。 Here is a sample class that does exactly this: 这里是一个示例类,正是这样做的:

@interface DeviceInfo : NSObject

+ (NSString *)model;

@end

#import "DeviceInfo.h"
#import <sys/utsname.h>

@implementation DeviceInfo

+ (NSString *)model
{
    struct utsname systemInfo;
    uname(&systemInfo);

    return [NSString stringWithCString: systemInfo.machine encoding: NSUTF8StringEncoding];
}

@end

In swift side: 在迅速方面:

let deviceModel = DeviceInfo.model()

#3楼

I made this "pure Swift" extension on UIDevice . 我在UIDevice上做了这个“纯Swift”扩展。

This version works in Swift 2 or higher If you use an earlier version please use an older version of my answer . 此版本适用于Swift 2或更高版本。如果您使用的是较早版本,请使用我的答案的较旧版本

If you are looking for a more elegant solution you can use my µ-framework DeviceKit published on GitHub (also available via CocoaPods, Carthage and Swift Package Manager). 如果您正在寻找更优雅的解决方案,则可以使用在GitHub上发布的µ框架DeviceKit (也可以通过CocoaPods,Carthage和Swift Package Manager获得)。

Here's the code: 这是代码:

import UIKit

public extension UIDevice {

    static let modelName: String = {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }

        func mapToDevice(identifier: String) -> String { // swiftlint:disable:this cyclomatic_complexity
            #if os(iOS)
            switch identifier {
            case "iPod5,1":                                 return "iPod touch (5th generation)"
            case "iPod7,1":                                 return "iPod touch (6th generation)"
            case "iPod9,1":                                 return "iPod touch (7th generation)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值