最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift获取iOS设备类型和iOS版本

iOS crifan 4153浏览 0评论

swift get ios device type

Better Code: Determine Device Types and iOS Versions – TechRepublic

Determine iPhone and iPad Hardware Machine and Device Type iOS(developer: tips)

Swift Version of getting the Device information… Tuple based.. provides Device type, description and optional version number

iOS device checks for OS version and screen size in Swift

ios – How to get the device type in Swift TodayExtension – Stack Overflow

osx – How to determine device type from Swift? (OS X or iOS) – Stack Overflow

ios – Detect current device with UI_USER_INTERFACE_IDIOM() in Swift – Stack Overflow

iOS: How to determine iphone model in Swift? – Stack Overflow

Models – The iPhone Wiki

【总结】

    let deviceModel = UIDevice.currentDevice().model
    print("deviceModel=\(deviceModel)")
    let localizedModel = UIDevice.currentDevice().localizedModel
    print("localizedModel=\(localizedModel)")
    let deviceName = UIDevice.currentDevice().name
    print("deviceName=\(deviceName)")
   
    let iOSVersion = UIDevice.currentDevice().systemVersion
    print("iOSVersion=\(iOSVersion)")
    let deviceDesc = UIDevice.currentDevice().description
    print("deviceDesc=\(deviceDesc)")
   
    let systemName = UIDevice.currentDevice().systemName
    print("systemName=\(systemName)")

对于iOS 模拟器:iPhone 5,的输出是:

deviceModel=iPhone
localizedModel=iPhone
deviceName=iPhone Simulator
iOSVersion=9.3
deviceDesc=<UIDevice: 0x7883e340>
systemName=iPhone OS

对于真机iPhone 6的输出是:

deviceModel=iPhone
localizedModel=iPhone
deviceName=Crifan iPhone6
iOSVersion=9.3
deviceDesc=<UIDevice: 0x144d14fc0>
systemName=iPhone OS

目前用如下代码:

/***************************************************************************
 * Device functions
 ***************************************************************************/

public enum DeviceModel: String {
    //case Simulator = "Simulator/sandbox",
    case Simulator = "Simulator",
    iPod1          = "iPod 1",
    iPod2          = "iPod 2",
    iPod3          = "iPod 3",
    iPod4          = "iPod 4",
    iPod5          = "iPod 5",
    iPod6          = "iPod 6",
    iPad2          = "iPad 2",
    iPad3          = "iPad 3",
    iPad4          = "iPad 4",
    iPhone1G       = "iPhone 1G",
    iPhone3G       = "iPhone 3G",
    iPhone3GS      = "iPhone 3GS",
    iPhone4        = "iPhone 4",
    iPhone4S       = "iPhone 4S",
    iPhone5        = "iPhone 5",
    iPhone5S       = "iPhone 5S",
    iPhone5C       = "iPhone 5C",
    iPadMini1      = "iPad Mini 1",
    iPadMini2      = "iPad Mini 2",
    iPadMini3      = "iPad Mini 3",
    iPadMini4      = "iPad Mini 4",
    iPadAir1       = "iPad Air 1",
    iPadAir2       = "iPad Air 2",
    iPadPro        = "iPad Pro",
    iPhone6        = "iPhone 6",
    iPhone6plus    = "iPhone 6 Plus",
    iPhone6S       = "iPhone 6S",
    iPhone6Splus   = "iPhone 6S Plus",
    AppleTV        = "Apple TV",
    Unknown        = "Unknown"
}

//public extension UIDevice {
extension UIDevice {
//    public var curDeviceType: DeviceModel {
    var curDeviceType: DeviceModel {
        var systemInfo = utsname()
        print("systemInfo=\(systemInfo)")
        uname(&systemInfo)
        print("systemInfo=\(systemInfo)")
        let modelCode = withUnsafeMutablePointer(&systemInfo.machine) {
            ptr in String.fromCString(UnsafePointer<CChar>(ptr))
        }
        print("modelCode=\(modelCode)")
        //modelCode=Optional("x86_64")
        //modelCode=Optional("iPhone7,2")
        var modelMap : [ String : DeviceModel ] = [
            "i386"      : .Simulator,
            "x86_64"    : .Simulator,

            "iPod1,1"  : .iPod1,
            "iPod2,1"  : .iPod2,
            "iPod3,1"  : .iPod3,
            "iPod4,1"  : .iPod4,
            "iPod5,1"  : .iPod5,
            "iPod7,1"  : .iPod6,

            "iPad2,1"  : .iPad2,
            "iPad2,2"  : .iPad2,
            "iPad2,3"  : .iPad2,
            "iPad2,4"  : .iPad2,
            "iPad2,5"  : .iPadMini1,
            "iPad2,6"  : .iPadMini1,
            "iPad2,7"  : .iPadMini1,
            "iPad3,1"  : .iPad3,
            "iPad3,2"  : .iPad3,
            "iPad3,3"  : .iPad3,
            "iPad3,4"  : .iPad4,
            "iPad3,5"  : .iPad4,
            "iPad3,6"  : .iPad4,
            "iPad4,1"  : .iPadAir1,
            "iPad4,2"  : .iPadAir2,
            "iPad4,4"  : .iPadMini2,
            "iPad4,5"  : .iPadMini2,
            "iPad4,6"  : .iPadMini2,
            "iPad4,7"  : .iPadMini3,
            "iPad4,8"  : .iPadMini3,
            "iPad4,9"  : .iPadMini3,
            "iPad5,1"  : .iPadMini4,
            "iPad5,2"  : .iPadMini4,
            "iPad6,7"  : .iPadPro,
            "iPad6,8"  : .iPadPro,
           
            "iPhone1,1" : .iPhone1G,
            "iPhone1,2" : .iPhone3G,
            "iPhone2,1" : .iPhone3GS,
            "iPhone3,1" : .iPhone4,
            "iPhone3,2" : .iPhone4,
            "iPhone3,3" : .iPhone4,
            "iPhone4,1" : .iPhone4S,
            "iPhone5,1" : .iPhone5,
            "iPhone5,2" : .iPhone5,
            "iPhone5,3" : .iPhone5C,
            "iPhone5,4" : .iPhone5C,
            "iPhone6,1" : .iPhone5S,
            "iPhone6,2" : .iPhone5S,
            "iPhone7,1" : .iPhone6plus,
            "iPhone7,2" : .iPhone6,
            "iPhone8,1" : .iPhone6S,
            "iPhone8,2" : .iPhone6Splus,
           
            "AppleTV2,1" : .AppleTV,
            "AppleTV3,1" : .AppleTV,
            "AppleTV3,2" : .AppleTV,
            "AppleTV5,3" : .AppleTV,
        ]
       
        if let model = modelMap[String.fromCString(modelCode!)!] {
            print("model=\(model)")
            //model=Simulator
            //model=iPhone6
            return model
        }

        return DeviceModel.Unknown
    }
   
    public var isPhone:Bool {
        return self.curDeviceType.rawValue.containsString("iPhone")
    }
   
    public var isPad:Bool {
        return self.curDeviceType.rawValue.containsString("iPad")
    }

    public var isPod:Bool {
        return self.curDeviceType.rawValue.containsString("iPod")
    }
   
    public var isSimulator:Bool {
        return self.curDeviceType.rawValue.containsString("Simulator")
    }

}
        var phoneType = UIDevice.currentDevice().curDeviceType.rawValue
        print("phoneType=\(phoneType)")
        //phoneType=iPhone 6
        phoneType = phoneType.replace(" ", to: "")
        print("phoneType=\(phoneType)")
        //iPhone6
        phoneType = phoneType.lowercaseString
        print("phoneType=\(phoneType)")
        //iphone6

        var iOSVersion = UIDevice.currentDevice().systemVersion
        print("iOSVersion=\(iOSVersion)")
        //9.2

        //9.3

转载请注明:在路上 » [已解决]swift获取iOS设备类型和iOS版本

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.266 seconds, using 22.17MB memory