Skip to content

Instantly share code, notes, and snippets.

@hboon
Last active October 15, 2017 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hboon/ecf20780cb3efa23cdea54b3b84f9068 to your computer and use it in GitHub Desktop.
Save hboon/ecf20780cb3efa23cdea54b3b84f9068 to your computer and use it in GitHub Desktop.
Use appledevicenames.com API to include the device name in support emails
//Using appledevicenames.com in Swift
//hboon@motionobj.com
//http://appledevicenames.com
//Stick this somewhere
private func deviceModel() -> String? {
var keys = [CTL_HW, HW_MACHINE]
var requiredSize = 0
let fetchSizeResult = sysctl(&keys, UInt32(keys.count), nil, &requiredSize, nil, 0)
guard fetchSizeResult == 0 else { return nil }
var data = [CChar](repeating: 0, count: requiredSize)
let fetchResult = sysctl(&keys, UInt32(keys.count), &data, &requiredSize, nil, 0)
guard fetchResult == 0 else { return nil }
return String(validatingUTF8: data)
}
//Stick this somewhere
func withDeviceName(block: @escaping (String) -> Void) {
let fallbackName = "Unknown"
if let model = deviceModel(),
let url = URL(string: "https://appledevicenames.com/device/\(model)") {
URLSession.shared.dataTask(with: url) { data, _ , _ in
DispatchQueue.main.async {
if let data = data, let name = String(data: data, encoding: .utf8) {
block(name)
} else {
block(fallbackName)
}
}
}.resume()
} else {
block(fallbackName)
}
}
//Calling code
withDeviceName { deviceName in
let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString")
let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion")
let osVersion = UIDevice.current.systemVersion
if let appVersion=appVersion, let buildNumber=buildNumber {
let body = "For info/diagnostic purposes:\n App v\(appVersion) build \(buildNumber) on \(deviceName), iOS \(osVersion)"
//body = "For info/diagnostic purposes:\n App v1.0.0 build 23 on iPhone X, iOS 11.0.3"
//Use body
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment