AssistantKit 0.7.1

AssistantKit 0.7.1

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2020年10月
SPM支持 SPM

anatoliyv 维护。



AssistantKit

CI Status Version License Platform

轻松检测设备环境

帮助

安装

AssistantKit 通过 CocoaPods 提供。要安装它,只需在 Podfile 中添加以下行:

pod "AssistantKit"

使用

设备版本信息

要获取当前设备类型,使用

let device = Device.type

switch device {
case .phone:      print("iPhone")
case .pad:        print("iPad")
case .pod:        print("iPod")
case .simulator:  print("Simulator")
default:          print("Unknown")
}

您可以使用以下代码检查确切的设备版本。所有的 version 值都可以在 Version 泛型中找到,位于 Version.swift 文件中。

switch Device.version {
case .phone5C:      print("iPhone 5C")
case .phone6:       print("iPhone 6")
case .phone6S:      print("iPhone 6S")
case .phone6Plus:   print("iPhone 6 Plus")
case .phone6SPlus:  print("iPhone 6 S Plus")
// and more iPhones

case .padAir:       print("iPad Air")
case .padAir2:      print("iPad Air 2")
case .padMini:      print("iPad Mini")
case .padPro:       print("iPad Pro")
// and more iPads

case .podTouch6:    print("iPod 6")
// and more iPods

case .simulator:    print("Simulator")
default:            print("Unknown device")
}

有一些属性可以检测设备类型

Device.isPhone     // true for iPhones even if it's Simulator
Device.isPad       // true for iPads even if it's Simulator
Device.isPadPro    // true for iPad Pros even if it's Simulator
Device.isPod       // true for iPods
Device.isSimulator // true for Simulators
Device.isNotched   // true for newer device version with notch

要获取原始设备代码使用

Device.versionCode

设备屏幕参数

可以使用以下代码检测屏幕大小。所有可能值都在位于 Screen.swift 中的 Screen 枚举中。

switch Device.screen {
case .inches_4_7:  print("4.7 inches")
case .inches_5_5:  print("5.5 inches")
case .inches_7_9:  print("7.9 inches")
case .inches_9_7:  print("9.7 inches")
case .inches_12_9: print("12.9 inches")
default:           print("Other display")
}

比较屏幕

Screen 确认实现了 Comparable 协议

Screen.inches_3_5 < Screen.inches_4_0 // Will be `true`

检测屏幕系列

通常需要根据特定的屏幕分辨率分配不同的参数。有3种方法可以帮助您检测使用哪些参数。但首先,让我先介绍一下 ScreenFamily

这是一个枚举,将所有可能的屏幕分为3组

  • .old:模拟3.5和4.0英寸屏幕的旧iPhone
  • .small:没有iPhone 6 Plus的其他iPhone/iPod
  • .medium:iPhone 6 Plus和iPad Mini
  • .big:iPad和iPad Pro

您可以通过以下方式检测屏幕系列

let family = Device.screen.family

现在让我们回到方法上

按设备类型赋值

要为iPhone和iPad设备分配不同的值,可以使用

// Method definition
static public func size<T: Any>(phone phone: T, pad: T) -> T

// Usage example
let size = Device.size(phone: 13, pad: 15)
let font = UIFont(name: "Arial", size: CGFloat(size))

在iPhone上,您的字体大小将为13.0,在iPad上为15.0

按ScreenFamily赋值

这是基于 ScreenFamily 的另一种方法

// Method definition
static public func size<T: Any>(small small: T, medium: T, big: T) -> T

// Usage example
let otherSize = Device.size(small: 12, medium: 14, big: 15)
let otherFont = UIFont(name: "Arial", size: CGFloat(otherSize))

在这种情况下,对于小屏幕,您的字体将为12.0,对于中等屏幕为14.0,对于大屏幕为15.0英寸。

重要提示:默认情况下,如果无法检测到屏幕系列,则 size 方法将分配较小的值。

根据精确屏幕尺寸取值

您也可以返回特定屏幕尺寸的值。您可以使用另一种尺寸方法。传入参数应该是屏幕尺寸。如果未定义,则将使用最接近的值。代码示例

// Method definition
static public func size<T: Any>(sizes sizes: [Screen : T]) -> T?

// Usage example
let sizes: [Screen: AnyObject] = [
   .inches_3_5: 12,
   .inches_4_0: 13,
   .inches_4_7: 14,
   .inches_9_7: 15
  ]
let exactSize = Device.size(sizes: sizes) as! Int
let _ = UIFont(name: "Arial", size: CGFloat(exactSize))

重要提示: 如果您传递一个空数组作为参数,该方法可能会返回 null。请注意隐式解包。

之后您的字体将设置为

  • 3.5英寸(旧设备)对应12
  • iPhone 5,5S对应13
  • iPhone 6,6Plus和iPad mini对应14
  • 以及其他iPad对应15

屏幕比例

switch Device.scale {
case .x1: print("Not retina")
case .x2: print("Retina 2X")
case .x3: print("Retina 3X")
default:  print("Unknown scale")
}

还有一个属性可以检测是否为-retina-显示屏

Device.isRetina // true if device screen scale > 1.0

界面方向

有两个属性可以帮助您了解当前方向

Device.isLandscape // true if landscape
Device.isPortrait  // true if portrait

Slide Over / iPad的多任务布局

要检测iPad上的Slide Over布局,只需调用

Device.isSlideOverLayout // true if iPad is in multitasking / slide over layout

检测和比较iOS版本

您可以在运行时检测iOS版本。有一些不同方法可以帮助您完成此操作

Device.osVersion                               // Current version as a `OSVersion` model

Device.osVersion == Device.os12                // true if iOS 12.0
Device.osVersion >= Device.os9                 // true if iOS >= 9.0
Device.osVersion < Device.os11                 // true if iOS < 11.0
etc.

以下常量表示主要iOS版本

Device.os8
Device.os9
Device.os10
Device.os11
Device.os12

与目录协作

有一些辅助方法可以更方便地访问文档和缓存目录。请参阅代码示例

Bundle.documentsDirectoryURL           // URL to .DocumentDirectory
Bundle.documentsDirectoryPath          // Path to .DocumentDirectory
Bundle.cachesDirectoryURL              // URL to .CachesDirectory
Bundle.cachesDirectoryPath             // Path to .CachesDirectory

let filePath = "directory/filename.txt"
Bundle.filePathInDocumentsDirectory(toFile: filePath)  // Path to file in .DocumentDirectory
Bundle.filePathInCachesDirectory(toFile: filePath)     // Path to file in .CachesDirectory

环境

用于检测环境选项。目前只有一个属性

/// Return `true` if running unit tests
Environment.isRunningUnitTests

电池

可以通过这些方法获取电池状态和电量。这将启用监控,如果尚未启用的话。

Device.Battery.state // Represent `UIDeviceBatteryState`
Device.Battery.level // in range 0...1 -1 for simulators

待办事项

  • 添加 tvOS 支持

如果你有想法,想要在此存储库中实现其他功能,请与我联系或发起 pull request。

作者

更多设备检测库

许可协议

AssistantKit基于MIT许可协议提供。更多信息请参阅LICENSE文件。