一个用 Swift 编写的运行时检查的集合。
换句话说,简单地这样做
if Defines.Device.isSimulator {
//Don't ask permission to send push notifications
}
我们都知道苹果公司做事的方式™,对吧? 不是检查设备,而是检查功能。 例如,与其检查设备是否为 iPod touch 并因此没有 GPS,不如检查 CoreLocation 是否可用。 或者,与其一直检查设备的屏幕尺寸,不如依靠 Auto Layout 的尺寸类等等。
我明白了。
但有时你必须在某个地方设置该标志。 Compact 和 Regular 无法捕捉 iPad Pro 12.9 英寸屏幕的实际空间有多大。 所以你可能想在那里呈现更多内容。 或者你可能只是不想在模拟器上请求推送通知的权限。 我们都经历过。
所以只需 import Defines 并继续你的生活。
请注意,Defines 不提供编译时安全检查。 它仅仅是用于运行时检查的静态布尔值的集合。
换句话说,这样做
let iOS9 = Defines.Version(versionString: "9.0")
if Defines.OS.isiOS && Defines.OS.version = iOS9 {
//execute code only available on iOS9
}
... 将不会消除任何编译器警告。
为此,请查看 检查 API 可用性 和 条件编译块。
只是为了让你了解它的要点。
Defines: 与此模块交互的主要入口点。设备
currentModel : 运行你的代码的设备型号。isSimulatorModel: 自 2008 年以来所有相关的 Apple 产品的型号标识符枚举。isAppleTVisAppleWatchisiPadisiPhoneisiPodisMacScreen: 有关你的应用程序可用的当前屏幕的信息。bounds: 屏幕的当前边界矩形。currentWidthcurrentHeightmaxLength: 宽度和高度之间的较大者。minLength: 宽度和高度之间的较小者。OS: 有关运行你的应用程序的操作系统的信息。version: 描述运行你的应用程序的操作系统版本,并使其易于比较。App: 你的应用程序,主要从 Info.plist 读取。name(forClass:): 查找你的应用程序的名称。version(forClass:): 查找你的应用程序的版本。Version: 定义软件版本,格式为 major.minor.patch (build)。Bundle:
id(forClass:): 查找你的应用程序的 bundle id。pod 'Defines', '~> 1.0'
然后在需要的地方 import Defines。
github "BellAppLab/Defines" ~> 1.0
然后在需要的地方 import Defines。
dependencies: [
.package(url: "https://github.com/BellAppLab/Defines", from: "1.0")
]
然后在需要的地方 import Defines。
cd toYourProjectsFolder
git submodule add -b submodule --name Defines https://github.com/BellAppLab/Defines.git
然后将 Definess 文件夹拖到你的 Xcode 项目中。
Bell App Lab, apps@bellapplab.com
Logo image by Sumana Chamrunworakiat from The Noun Project
Defines 在 MIT 许可下可用。 有关更多信息,请参见 LICENSE 文件。