按照@HAShere的非常好的回答,你可以知道设备的型号。让我们创建一个名为DeviceModel 的快速文件,以保持扩展的各个部分分开。
DeviceModel.swift
import UIKit
private let DeviceList = [
/* iPod 5 */ "iPod5,1": "iPod Touch 5",
/* iPhone 4 */ "iPhone3,1": "iPhone 4", "iPhone3,2": "iPhone 4", "iPhone3,3": "iPhone 4",
/* iPhone 4S */ "iPhone4,1": "iPhone 4S",
/* iPhone 5 */ "iPhone5,1": "iPhone 5", "iPhone5,2": "iPhone 5",
/* iPhone 5C */ "iPhone5,3": "iPhone 5C", "iPhone5,4": "iPhone 5C",
/* iPhone 5S */ "iPhone6,1": "iPhone 5S", "iPhone6,2": "iPhone 5S",
/* iPhone 6 */ "iPhone7,2": "iPhone 6",
/* iPhone 6 Plus */ "iPhone7,1": "iPhone 6 Plus",
/* iPad 2 */ "iPad2,1": "iPad 2", "iPad2,2": "iPad 2", "iPad2,3": "iPad 2", "iPad2,4": "iPad 2",
/* iPad 3 */ "iPad3,1": "iPad 3", "iPad3,2": "iPad 3", "iPad3,3": "iPad 3",
/* iPad 4 */ "iPad3,4": "iPad 4", "iPad3,5": "iPad 4", "iPad3,6": "iPad 4",
/* iPad Air */ "iPad4,1": "iPad Air", "iPad4,2": "iPad Air", "iPad4,3": "iPad Air",
/* iPad Air 2 */ "iPad5,1": "iPad Air 2", "iPad5,3": "iPad Air 2", "iPad5,4": "iPad Air 2",
/* iPad Mini */ "iPad2,5": "iPad Mini", "iPad2,6": "iPad Mini", "iPad2,7": "iPad Mini",
/* iPad Mini 2 */ "iPad4,4": "iPad Mini", "iPad4,5": "iPad Mini", "iPad4,6": "iPad Mini",
/* iPad Mini 3 */ "iPad4,7": "iPad Mini", "iPad4,8": "iPad Mini", "iPad4,9": "iPad Mini",
/* Simulator */ "x86_64": "Simulator", "i386": "Simulator"
]
public extension UIDevice {
var modelName: String {
var systemInfo = utsname()
uname(&systemInfo)
let machine = systemInfo.machine
let mirror = reflect(machine) // Swift 1.2
// let mirror = Mirror(reflecting: machine) // Swift 2.0
var identifier = ""
// Swift 1.2 - if you use Swift 2.0 comment this loop out.
for i in 0..<mirror.count {
if let value = mirror[i].1.value as? Int8 where value != 0 {
identifier.append(UnicodeScalar(UInt8(value)))
}
}
// Swift 2.0 and later - if you use Swift 2.0 uncomment his loop
// for child in mirror.children where child.value as? Int8 != 0 {
// identifier.append(UnicodeScalar(UInt8(child.value as! Int8)))
// }
return DeviceList[identifier] ?? identifier
}
}
然后在didFinishLaunchingWithOptions 中的AppDelegate.swift 中,您可以实例化有关设备型号的故事板,如下所示:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var storyboard: UIStoryboard
// instantiate the `UIWindow`
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// Get the model name based in the extension.
let modelName = UIDevice.currentDevice().modelName
// If the modelName variable contains the string "iPhone 6" inside.
if (modelName.rangeOfString("iPhone 6") != nil) {
storyboard = UIStoryboard(name: "iPhone6Storyboard", bundle: nil)
self.window!.rootViewController = storyboard.instantiateInitialViewController() as! iPhone6ViewController
}
else if (modelName.rangeOfString("iPad") != nil) {
storyboard = UIStoryboard(name: "iPadStoryboard", bundle: nil)
self.window!.rootViewController = storyboard.instantiateInitialViewController() as! iPadViewController
}
self.window!.makeKeyAndVisible()
return true
}
如果您添加了所需的UIStoryboard,则上述代码有效,使用Interface Builder 为每个UIStoryboard 设置其初始UIViewController(如果您不想设置初始@987654331 @使用Interface Builder,您可以很容易地找到如何以编程方式设置初始UIViewController,但为了简洁起见,我使用了Interface Builder。
当然,您需要在应用程序的设置中,转到您的目标和Info 选项卡。有清除Main storyboard file base name 的值。在General 选项卡上,清除主界面的值。您可以在此答案Programmatically set the initial view controller using Storyboards 中了解更多信息。
你需要注意两件事:
- 在模拟器中你不能测试设备模型代码(它只给你'模拟器'),你需要真实的设备。
- 您需要根据
DeviceModel.swift中的代码正确使用设备名称以匹配不同的名称。
为简洁起见,我只在上面放了两个设备,但无论如何你需要根据名称匹配你想要的任何设备型号。
希望对你有所帮助。