Last active
August 29, 2015 14:27
-
-
Save malcolmteas/ddfc8fc9440df1808efd to your computer and use it in GitHub Desktop.
Get a more human-readable device model name. So get the "iPhone5,1" type format and translate it into "iPhone 5" or whatever.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Sample functions to use to get the machine name (aka device model) in iOS 8 | |
Use this by copying these methods into your project in the appropriate place. | |
Clearly this is not an actual class file. Likely at some point I'll make | |
a UIDevice category that has this and other handy functions. | |
Malcolm Teas, August 2015 */ | |
/** | |
Raw device model method | |
Returns a string of the raw device model like "iphone5,1" Not much use | |
for humans since no one really remembers these values well. But useful for below. */ | |
func getRawDeviceModel() -> String { | |
var size: size_t = 0 | |
sysctlbyname("hw.machine", nil, &size, nil, 0); | |
var machine = [CChar](count: Int(size), repeatedValue: 0) | |
sysctlbyname("hw.machine", &machine, &size, nil, 0); | |
return String.fromCString(machine)! | |
} | |
/** | |
Human readable-string method | |
Returns a human-useful string of the iPhone model name based on the raw name. */ | |
func getDeviceModel() -> String { | |
let model = getRawDeviceModel() | |
/* This is Swift code, so we don't need to handle anything below iPhone 4. | |
See http://theiphonewiki.com/wiki/IPhone for reference to update. This | |
will have to be udpated ever fall when Apple releases new phones. */ | |
switch model { | |
case "iPhone3,1": return "iPhone 4" | |
case "iPhone3,2": return "iPhone 4" | |
case "iPhone3,3": return "iPhone 4" | |
case "iPhone4,1": return "iPhone 4S" | |
case "iPhone5,1": return "iPhone 5" | |
case "iPhone5,2": return "iPhone 5" | |
case "iPhone5,3": return "iPhone 5C" | |
case "iPhone5,4": return "iPhone 5C" | |
case "iPhone6,1": return "iPhone 5S" | |
case "iPhone6,2": return "iPhone 5S" | |
case "iPhone7,2": return "iPhone 6+" | |
case "iPhone7,1": return "iPhone 6" | |
default: return "-" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment