ios swift 获取设备名称

一直在网上看到的都是oc 的获取设备名称, 然后自己想找一个swift 方式的去获取, 然后找到了一个外国人的帖子,发现ok就转载下



extension UIDevice{

///设备型号的名称

var modelName:String{

var systemInfo =utsname()

uname(&systemInfo)

letmachineMirror =Mirror(reflecting: systemInfo.machine)

let identifier = machineMirror.children.reduce("") { identifier, elementin

guardletvalue = element.valueas?Int8wherevalue !=0else{returnidentifier }

returnidentifier +String(UnicodeScalar(UInt8(value)))

}

switchidentifier {

case"iPod5,1":return"iPod Touch 5"

case"iPod7,1":return"iPod Touch 6"

case"iPhone3,1","iPhone3,2","iPhone3,3":return"iPhone 4"

case"iPhone4,1":return"iPhone 4s"

case"iPhone5,1","iPhone5,2":return"iPhone 5"

case"iPhone5,3","iPhone5,4":return"iPhone 5c"

case"iPhone6,1","iPhone6,2":return"iPhone 5s"

case"iPhone7,2":return"iPhone 6"

case"iPhone7,1":return"iPhone 6 Plus"

case"iPhone8,1":return"iPhone 6s"

case"iPhone8,2":return"iPhone 6s Plus"

case"iPad2,1","iPad2,2","iPad2,3","iPad2,4":return"iPad 2"

case"iPad3,1","iPad3,2","iPad3,3":return"iPad 3"

case"iPad3,4","iPad3,5","iPad3,6":return"iPad 4"

case"iPad4,1","iPad4,2","iPad4,3":return"iPad Air"

case"iPad5,3","iPad5,4":return"iPad Air 2"

case"iPad2,5","iPad2,6","iPad2,7":return"iPad Mini"

case"iPad4,4","iPad4,5","iPad4,6":return"iPad Mini 2"

case"iPad4,7","iPad4,8","iPad4,9":return"iPad Mini 3"

case"iPad5,1","iPad5,2":return"iPad Mini 4"

case"iPad6,7","iPad6,8":return"iPad Pro"

case"AppleTV5,3":return"Apple TV"

case"i386","x86_64":return"Simulator"

default:returnidentifier

}

}

}

这样扩展下 用起来也很方便

let modelName=UIDevice.currentDevice().modelName

排版不行 来张图


ios swift 获取设备名称_第1张图片


extensionUIDevice{

///设备型号的名称

varmodelName:String{

varsystemInfo =utsname()

uname(&systemInfo)

varidentifier =NSString(bytes: &systemInfo.machine, length:Int(_SYS_NAMELEN), encoding:NSUTF8StringEncoding)!asString//会有n多\0

// mark不支持ios 7换种写法

//var identifier = "iPhone"

//if #available(iOS 8.0, *){

//let machineMirror = Mirror(reflecting: systemInfo.machine)

//identifier = machineMirror.children.reduce("") { identifier, element in

//guard let value = element.value as? Int8 where value != 0 else { return identifier }

//return identifier + String(UnicodeScalar(UInt8(value)))

//}

//}else{

//return "iPhone"

//}

funcseach(str:String)->Bool{

ifidentifier.rangeOfString(str) !=nil{

returntrue

}

returnfalse

}

switchtrue{

caseseach("iPhone8,4"):return"iPhone SE"

caseseach("iPhone4,1"):return"iPhone 4s"

caseseach("iPhone5,1"),seach("iPhone5,2"):return"iPhone 5"

caseseach("iPhone5,3"),seach("iPhone5,4"):return"iPhone 5c"

caseseach("iPhone6,1"),seach("iPhone6,2"):return"iPhone 5s"

caseseach("iPhone7,2"):return"iPhone 6"

caseseach("iPhone7,1"):return"iPhone 6 Plus"

caseseach("iPhone8,1"):return"iPhone 6s"

caseseach("iPhone8,2"):return"iPhone 6s Plus"

caseseach("iPad2,1"),seach("iPad2,2"),seach("iPad2,3"),seach("iPad2,4"):return"iPad 2"

caseseach("iPad3,1"),seach("iPad3,2"),seach("iPad3,3"):return"iPad 3"

caseseach("iPad3,4"),seach("iPad3,5"),seach("iPad3,6"):return"iPad 4"

caseseach("iPad4,1"),seach("iPad4,2"),seach("iPad4,3"):return"iPad Air"

caseseach("iPad5,3"),seach("iPad5,4"):return"iPad Air 2"

caseseach("iPad2,5"),seach("iPad2,6"),seach("iPad2,7"):return"iPad Mini"

caseseach("iPad4,4"),seach("iPad4,5"),seach("iPad4,6"):return"iPad Mini 2"

caseseach("iPad4,7"),seach("iPad4,8"),seach("iPad4,9"):return"iPad Mini 3"

caseseach("iPad5,1"),seach("iPad5,2"):return"iPad Mini 4"

caseseach("iPad6,7"),seach("iPad6,8"):return"iPad Pro"

caseseach("iPad6,4"),seach("iPad6,3"):return"iPad Pro(9.7)"

caseseach("iPhone3,1") ,seach("iPhone3,2") ,seach("iPhone3,3"):return"iPhone 4"

caseseach("iPod5,1"):return"iPod Touch 5"

caseseach("iPod7,1"):return"iPod Touch 6"

caseseach("AppleTV5,3"):return"Apple TV"

caseseach("i386"),seach("x86_64"):return"Simulator"

//预留的默认信息

caseseach("iPod") :return"iPod"

caseseach("iPad"):return"iPad"

caseseach("iPhone"):return"iPhone"

default:

return"iPhone"

}

}

}


-----------新方式觉得还不错,代码结构更优秀 -------------

publicenumModel :String{

//case simulator = "iPhone",

caseiPod1="iPod 1",

iPod2="iPod 2",

iPod3="iPod 3",

iPod4="iPod 4",

iPod5="iPod 5",

iPad2="iPad 2",

iPad3="iPad 3",

iPad4="iPad 4",

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",

iPadAir1="iPad Air 1",

iPadAir2="iPad Air 2",

iPhone6="iPhone 6",

iPhone6plus="iPhone 6 Plus",

iPhone6S="iPhone 6S",

iPhone6Splus="iPhone 6S Plus",

unrecognized="iPhone"

}

publicextensionUIDevice{

publicvarmodelName:Model{

varsystemInfo = utsname()

uname(&systemInfo)

letmachineMirror = Mirror(reflecting: systemInfo.machine)

letidentifier = machineMirror.children.reduce("") { identifier, elementin

guardletvalue = element.valueas?Int8, value !=0else{returnidentifier }

returnidentifier + String(UnicodeScalar(UInt8(value)))

}

varmodelMap : [String:Model] = [

"i386": .unrecognized,

"x86_64": .unrecognized,

"iPod1,1": .iPod1,

"iPod2,1": .iPod2,

"iPod3,1": .iPod3,

"iPod4,1": .iPod4,

"iPod5,1": .iPod5,

"iPad2,1": .iPad2,

"iPad2,2": .iPad2,

"iPad2,3": .iPad2,

"iPad2,4": .iPad2,

"iPad2,5": .iPadMini1,

"iPad2,6": .iPadMini1,

"iPad2,7": .iPadMini1,

"iPhone3,1": .iPhone4,

"iPhone3,2": .iPhone4,

"iPhone3,3": .iPhone4,

"iPhone4,1": .iPhone4S,

"iPhone5,1": .iPhone5,

"iPhone5,2": .iPhone5,

"iPhone5,3": .iPhone5C,

"iPhone5,4": .iPhone5C,

"iPad3,1": .iPad3,

"iPad3,2": .iPad3,

"iPad3,3": .iPad3,

"iPad3,4": .iPad4,

"iPad3,5": .iPad4,

"iPad3,6": .iPad4,

"iPhone6,1": .iPhone5S,

"iPhone6,2": .iPhone5S,

"iPad4,1": .iPadAir1,

"iPad4,2": .iPadAir2,

"iPad4,4": .iPadMini2,

"iPad4,5": .iPadMini2,

"iPad4,6": .iPadMini2,

"iPad4,7": .iPadMini3,

"iPad4,8": .iPadMini3,

"iPad4,9": .iPadMini3,

"iPhone7,1": .iPhone6plus,

"iPhone7,2": .iPhone6,

"iPhone8,1": .iPhone6S,

"iPhone8,2": .iPhone6Splus

]

ifletmodel = modelMap[identifier] {

returnmodel

}

returnModel.unrecognized

}

}


原帖网址http://stackoverflow.com/questions/26028918/ios-how-to-determine-iphone-model-in-swift

你可能感兴趣的:(ios swift 获取设备名称)