ios - How to determine the current iPhone/device model? -
is there way device model name (iphone 4s, iphone 5, iphone 5s, etc) in swift?
i know there property named uidevice.currentdevice().model
returns device type (ipod touch, iphone, ipad, iphone simulator, etc).
i know can done in objective-c method:
#import <sys/utsname.h> struct utsname systeminfo; uname(&systeminfo); nsstring* devicemodel = [nsstring stringwithcstring:systeminfo.machine encoding:nsutf8stringencoding];
but i'm developing iphone app in swift please me equivalent way solve in swift?
i made "pure swift" extension on uidevice
.
this version requires swift 2.0 if use earlier version please use an older version of answer.
if looking more elegant solution can use my ยต-framework devicekit
published on github (also available via cocoapods).
here's code:
import uikit public extension uidevice { var 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))) } switch identifier { 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 "iphone9,1", "iphone9,3": return "iphone 7" case "iphone9,2", "iphone9,4": return "iphone 7 plus" case "iphone8,4": return "iphone se" case "iphone10,1", "iphone10,4": return "iphone 8" case "iphone10,2", "iphone10,5": return "iphone 8 plus" case "iphone10,3", "iphone10,6": return "iphone x" 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 "ipad6,11", "ipad6,12": return "ipad 5" 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,3", "ipad6,4": return "ipad pro 9.7 inch" case "ipad6,7", "ipad6,8": return "ipad pro 12.9 inch" case "ipad7,1", "ipad7,2": return "ipad pro 12.9 inch 2. generation" case "ipad7,3", "ipad7,4": return "ipad pro 10.5 inch" case "appletv5,3": return "apple tv" case "appletv6,2": return "apple tv 4k" case "audioaccessory1,1": return "homepod" case "i386", "x86_64": return "simulator" default: return identifier } } }
you call this:
// swift 2 let modelname = uidevice.currentdevice().modelname // swift 3 let modelname = uidevice.current.modelname
Comments
Post a Comment