Swift: 讀取 iOS 設備的型號/版本資料
let dictionary = Bundle.main.infoDictionary!
let app_version = dictionary["CFBundleShortVersionString"] as! String
let app_build = dictionary["CFBundleVersion"] as! String
let app_dDisplayName = dictionary["CFBundleDisplayName"]
print("app_dDisplayName = \(app_dDisplayName!)")
print("app_version = \(app_version)")
print("app_build = \(app_build)")
print("UIDevice.current.name = \(UIDevice.current.name)")
print("UIDevice.current.model = \(UIDevice.current.model)")
print("UIDevice.current.systemName = \(UIDevice.current.systemName)")
print("UIDevice.current.systemVersion = \(UIDevice.current.systemVersion)")
print("UIDevice.current.identifierForVendor! = \(UIDevice.current.identifierForVendor!)")
UIDevice.current.identifierForVendor --> 當 app重裝後 identifierForVendor 會變
模擬器 的結果:
app_dDisplayName = helloApp
app_version = 1.0
app_build = 1
UIDevice.current.name = ffmac (Macbook的名稱)
UIDevice.current.model = iPhone
UIDevice.current.systemName = iOS
UIDevice.current.systemVersion = 11.2
UIDevice.current.identifierForVendor! = E6B52865-0F8E-4773-BF42-E40E2C37F472
iPhone 6 的結果:
app_dDisplayName = helloApp
app_version = 1.0
app_build = 1
UIDevice.current.name = _helloiphone
UIDevice.current.model = iPhone
UIDevice.current.systemName = iOS
UIDevice.current.systemVersion = 11.2.6
UIDevice.current.identifierForVendor! = 2057AEC2-91F6-44D4-8D32-36CDFE2638D0
另外,要得知 iOS 的設備型號
參考
https://stackoverflow.com/questions/24059327/detect-current-device-with-ui-user-interface-idiom-in-swift/40083017
let userInterface = UIDevice.current.userInterfaceIdiom if userInterface == .pad { print("iPad") } else if userInterface == .phone { print("iPhone") } else if userInterface == .carPlay { print("CartPlay") } else if userInterface == .tv { print("AppleTV") }
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 where 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 "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,3", "iPad6,4", "iPad6,7", "iPad6,8":return "iPad Pro" case "AppleTV5,3": return "Apple TV" case "i386", "x86_64": return "Simulator" default: return identifier } } } This is how you will use it: let modelName = UIDevice.currentDevice().modelName
另外
if #available(iOS 10.0, *) {
// iOS 10.0 (含)以上 或 其它作業系統 執行這裡
}
* 星號 指的是其它作業系統
或這樣寫
@available(iOS 10.0, OSX 10.10, *)
func myMethod() { // iOS 10.0 (含)以上 或 OSX 10.10(含)以上 或 其它作業系統 執行這裡
...
}
或這樣
@available(iOS 10.0, *)
func myMethod() {
...
}
還有
@available(swift 3.0.2)
struct MyStruct {
// struct definition
}
if #available(tvOS 1.0, *) {
}
if #available(watchOS 1.0, *) {
}
if #available(OSX 10.11, *) {
}
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Attributes.html
http://swift.gg/2016/04/13/swift-qa-2016-04-13/