UIDeviceComplete
为填充缺失部分的 UIDevice 扩展。
这个库是关于什么的?
UIDeviceComplete
是一个 iOS 库,旨在为 UIDevice
提供一系列扩展,这些扩展提供了 UIDevice
当前缺乏的功能,比如确定您拥有的 iOS 设备(例如 iPhone X、iPhone 8、iPhone SE、iPad Pro、iPhone 7 等)或确定设备屏幕的大小(以英寸为单位)。
功能
- 检测特定设备,如 iPhone 7、iPhone SE 或 iPad Pro
- 获取设备屏幕尺寸(以英寸为单位)
- 获取 iOS 设备系列(iPod、iPhone 或 iPad)
没有看到您需要的功能?
请随意开启一个请求该功能的Issue,或者发送一个pull request!
使用
所有的扩展都是基于dc
变量实现的,它代表一个(用户界面)设备完成对象,这个库是扩展到UIDevice
上的,这样就可以很容易地看到UIDevice
的原生方法和这个库的方法,同时也能降低命名冲突的可能性。
获取常见设备名称
UIDevice.current.dc.commonDeviceName // iPad Pro (12.9 inch, Wi-Fi+LTE)
检测iOS设备型号
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...")
}
检测iOS设备屏幕尺寸(英寸)
可以通过以下计算属性查询屏幕尺寸,该属性返回代表屏幕尺寸的简单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).")
}
检测iOS设备系列
如果您只关心设备系列类型,例如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
如果您使用 Carthage 来管理依赖项,只需将 UIDeviceComplete 添加到您的 Cartfile
github "Nirma/UIDeviceComplete"
如果您使用 Carthage 来构建依赖项,请确保您已将 UIDeviceComplete.framework
添加到目标的 "链接框架和库" 部分,并在 Carthage 框架复制构建阶段中包含 UIDeviceComplete.framework
CocoaPods
如果您使用 CocoaPods 来管理依赖项,只需将此行添加到您的 Podfile
use_frameworks!
pod 'UIDeviceComplete'
向本项目贡献力量
贡献受到热烈欢迎
如果您想修复有关项目的某些内容,或者想添加其他类型的增强功能,请提出将它们添加到项目的建议。请随时向该项目提交拉取请求或创建一个问题。
许可
UIDeviceComplete 在 MIT 许可下发布。请参阅 LICENSE 以获取详细信息。