UIDevice 扩展,填补缺失的功能。
UIDeviceComplete
是一个 iOS 库,旨在作为 UIDevice
的扩展集合,提供 UIDevice
目前缺乏的功能,例如确定您拥有哪种 iOS 设备(例如 iPhone X、iPhone 8、iPhone SE、iPad Pro、iPhone 7 等)或确定设备屏幕尺寸(英寸)。
随时提出 Issue 请求您想要的功能,或者发送 Pull Request!
所有的扩展都通过 dc
变量调用,它代表 ([UI]DeviceComplete) 对象,这个库扩展到 UIDevice
上,这样可以很容易地看到 UIDevice
的原生方法和这个库的方法,同时也降低了命名冲突的可能性。
UIDevice.current.dc.commonDeviceName // iPad Pro (12.9 inch, Wi-Fi+LTE)
let device = UIDevice.current.dc.deviceModel
switch device {
case .iPhoneX:
print("So hows that iPhone X notch thing working out? Right...")
case .iPhone6Plus, .iPhone7Plus:
print("Lots of screen realestate eh?")
case .iPhoneSE, .iPhone5, iPhone5S:
print("Less iPhone is more iPhone...")
case .iPadPro:
print("Why?")
default:
print("Not sure what this is...")
}
可以使用以下计算属性查询屏幕尺寸,该属性返回一个简单的 Double
,表示屏幕尺寸(英寸)
let screenSize: Double = UIDevice.current.dc.screenSize.sizeInches
if screenSize <= 4.0 {
print("Modest screen size; not so modest price tag")
} else if screenSize >= 5.5 {
print("Plus is always a plus")
} else {
print("Chances are this is an iPad or an iPhone (Texas Edition).")
}
如果您只需要设备系列的类型,例如 iPhone 或 iPad,而不需要特定的型号,那么 DeviceFamily
可能是您需要的。
let deviceFamily = UIDevice.current.dc.deviceFamily
switch deviceFamily {
case .iPhone:
print("...phone home?")
case .iPad:
print("when it comes to screen size; more is more")
case .iPod:
print("Why not?")
default:
print("No family...")
}
如果您使用 Carthage 管理您的依赖项,只需将 UIDeviceComplete 添加到您的 Cartfile
github "Nirma/UIDeviceComplete"
如果您使用 Carthage 构建您的依赖项,请确保您已将 UIDeviceComplete.framework
添加到目标中的 "Linked Frameworks and Libraries" 部分,并在您的 Carthage 框架复制构建阶段中包含了 UIDeviceComplete.framework
。
如果您使用 CocoaPods 管理您的依赖项,只需将此行添加到您的 Podfile
use_frameworks!
pod 'UIDeviceComplete'
非常欢迎贡献
如果您想修复项目中的某些问题,或者希望添加任何其他类型的增强功能,请提出添加到项目。 请随时发送 Pull Request 或为此项目打开一个 Issue。
UIDeviceComplete 基于 MIT 许可证发布。 查看 LICENSE 获取详细信息。