Device.swiftpm

Device 是一个值类型的替代方案,用于获取所有支持平台上的设备信息(非常类似于 DeviceKit,但设计上更容易维护)。Device 的定义包含清晰的初始化器,因此任何人都可以添加新设备并为此项目做出贡献,即使是在 iPad 上使用 Swift Playgrounds,而无需 Xcode。无需记忆映射模式或使用额外的构建工具。

主要目标是由多人轻松维护,采用可在所有平台上使用的一致 API,并且可以使用 iPad 和 macOS 上的 Swift Playgrounds 进行维护。API 通常即使在不支持所有功能的平台上也存在,因此无需在外部代码中执行可用性检查,并且在不相关的情况下,代码可以简单地返回可选值。

我们积极维护此项目,因此如果有功能请求或更改,我们将努力在一周内解决。

特性

要求

已知问题

为 macOS 构建的“专为 iPad 设计”返回 iPad 配置文件,而不是实际硬件配置文件。自定义符号可能无法在 macOS < 13 或 watchOS < 7 中工作。macOS < 12 中无法进行低功耗模式检查。有关更多已知问题和路线图,请参阅 CHANGELOG.md

安装

通过将其作为包依赖项添加到您的代码中进行安装。这可以在 Xcode 或 Swift Playgrounds 中完成!

Swift Package Manager

Swift 5+

您可以通过添加包 https://github.com/kudit/Device 在 Swift Playground 中尝试这些示例

如果存储库是私有的,请使用以下链接导入:https://<your-PAT-string>@github.com/kudit/Device.git

或者,您可以手动在 Package.swift 文件中输入以下内容

dependencies: [
    .package(url: "https://github.com/kudit/Device.git", from: "2.0.0"),
]

用法

首先,确保导入框架

import Device

以下是一些用法示例。

获取导入的 Device 的版本。

let version = Device.version

获取您正在运行的设备

let device = await Device.current // await required if not on the main thread (@MainActor isolated)

print(device) // prints, for example, "iPhone 6 Plus"

if device.has(.force3dTouch) {
    // do something that needs force-touch.
} else {
    // fallback for devices that do not support this.
}

if device.is(.plus) || device.is(.max) {
    // do something only available for "Plus" model devices.
}

if device.has(.battery) && device.has(.lidar) && device.has(.headphoneJack) {
    // do something only if there is a battery, lidar, and a headphoneJack
}

获取可以在 Hardware.swift 中的枚举 Capability 下查询的完整标志列表。

获取设备类型

let device = Device.current
if device.idiom == .pad {
  // iPad
} else if device.idiom == .phone {
  // iPhone
} else if device.idiom == .vision {
  // Apple Vision device
}

检查是否在模拟器中运行

if Device.isSimulator {
  // Running on one of the simulators
  // Skip doing something irrelevant for Simulator
} 

检查是否在预览中运行

if Device.isPreview {
  // Running in an XCode #Preview
} 

检查是否在 Playground 中运行

if Device.isPlayground {
  // Running in an XCode #Preview
} 

检查是否在物理设备上运行

if Device.isRealDevice {
  // Running on physical hardware and not a simulator
} 

获取当前电池状态

注意

在获取当前电池状态时,电池监控启用将暂时设置为 true,然后恢复为之前的状态,因此无需单独管理监控。如果您需要电池状态或电量变化时收到通知,您可以添加一个监视器,该监视器会在电量变化时调用您的代码。但是,通常可以将其作为 DeviceBattery 放入,因为它是一个 ObservableObject。

if let battery = Device.current.battery {
    // do things that need the battery
    if battery.currentState == .full || (battery.currentState == .charging && battery.currentLevel >= 75) {
        print("Your battery is happy! 😊")
    }
    
    // get the current battery level
    if battery.currentLevel >= 50 {
        install_iOS()
    } else {
        showLowBatteryWarning()
    }

    if battery.lowPowerMode {
        print("Low Power mode is enabled! 🔋")
    } else {
        print("Low Power mode is disabled! 😊")
    }

    // add monitor to do something whenever battery level changes (like updating UI)
    battery.addMonitor {
        localBatteryLevel = battery.currentLevel
        localBatteryState = battery.currentState
    }
} else {
    // handle behaviour on devices without a battery
}

获取当前电池电量

if let level = Device.current.battery?.currentLevel, level >= 50 {
  install_iOS()
} else {
  showError()
}

检查引导式访问会话当前是否处于活动状态

if Device.current.isGuidedAccessSessionActive {
  print("Guided Access session is currently active")
} else {
  print("No Guided Access session is currently active")
}

获取屏幕亮度

if Device.current.screenBrightness > 50 {
  print("Take care of your eyes!")
}

获取可用磁盘空间

if Device.current.volumeAvailableCapacityForOpportunisticUsage ?? 0 > Int64(1_000_000) {
  // download that nice-to-have huge file
}

if Device.current.volumeAvailableCapacityForImportantUsage ?? 0 > Int64(1_000) {
  // download that file you really need
}

禁用空闲计时器

Device.current.isIdleTimerDisabled = true // must be run on the main actor AFTER most of the UI is loaded (so do on a view onAppear and NOT during the app init)

// Disable automatically when plugged in.  Only call this once (This would be appropriate to call during init).
Device.current.disableIdleTimerWhenPluggedIn()

显示 BatteryView

// BatteryView() will default to a view with a live updating battery indicator.
BatteryView()
// you can have a larger one by changing the font size
BatteryView(fontSize: 80)

所有这些测试都可以使用预览或通过运行模块的 Development 文件夹中捆绑的应用程序可执行文件来演示。

信息来源

一些信息来自以下来源

贡献

如果您需要实现特定功能或遇到错误,请打开一个 issue。如果您自己扩展了功能并希望其他人也使用它,请提交一个 pull request。

捐赠

这是一项繁重的工作。如果您发现此内容有用,尤其是在商业产品中使用它,请考虑向 http://paypal.me/kudit 捐款

许可

您可以随意在项目中使用此代码,但是,请在应用程序中的某个位置添加回此项目的链接并注明出处。例如,用于版本的 Markdown 和字符串插值

Text("Open Source projects used include [Device](https://github.com/kudit/Device) v\(Device.version)

贡献者

贡献于此项目的完整人员列表可在此处 获得。非常感谢所有贡献者!🙏