swift-standard-clients

SwiftPM 5.3 Platforms @maximkrouk

标准 iOS 管理器的客户端声明和实时实现

关于客户端方法的更多信息

来源 描述
Brandon Williams - 协议见证 (Protocol Witnesses) 2019 App Builders Conference 演讲
Pointfree - 协议见证 (Protocol Witnesses) Pointfree 合集
pointfree/isowords 可以在这里找到不同客户端的示例

目录

描述 接口 实现
缓存 CacheClient MemoryCacheClient
IDFA IDFAPermissionsClient IDFAPermissionsClientLive
钥匙串 (Keychain) KeychainClient KeychainClientLive
通知 NotificationsPermissionsClient NotificationsPermissionsClientLive
触感引擎 (HapticEngine) HapticEngineClient HapticEngineClientLive
UserDefaults UserDefaultsClient UserDefaultsClientLive

待办事项

缓存

CacheClient<Key, Value> 是一个基于可哈希键值对的通用客户端,它为以下操作提供了接口

MemoryCacheClient

MemoryCacheClient 基于 NSCache 构建。 在底层,它使用 MemoryCache 包装器,这是 John Sundell 的 Cache 的改进版本。 您可以直接使用 MemoryCache (它还提供了一种将自身保存到磁盘的方法,如果您的类型是可编码的)来构建您自己的 CacheClient 实现。

IDFA

IDFAPermissionClientASIdentifierManagerATTrackingManager 的客户端,它为以下操作提供了接口

IDFAPermissionClient.AuthorizationStatusATTrackingManager.AuthorizationStatus 类型和 ASIdentifierManager.isAdvertisingTrackingEnabled 值的包装器,它的值是

它还有一个计算属性 isPermissive,对于 .authorized.unavailableWithTrackingEnabled 它是 true

钥匙串 (Keychain)

KeychainClient 是用于安全框架钥匙串访问的客户端,它**将对象存储为数据**(使用 DataRepresentable 协议),并为以下操作提供了接口

可以通过 rawValue: StirngStringLiteralStringInterpolation 初始化 KeychainClient.Key。 此外,您可以使用 .bundle(_:Key).bundle(_:Bundle, _:Key)bundleID 前缀添加到您的密钥。

KeychainClient.Operations.Save.AccessPolicy 是 kSec 访问常量的包装器,它的值是

通知

NotificationsPermissionsClientUNUserNotificationCenter 的客户端,它为以下操作提供了接口

NotificationsPermissionsClient.AuthorizationStatusUNAuthorizationStatus 类型的包装器,它的值是

它还有一个计算属性 isPermissive,对于 authorizedephimeralprovisional 它是 true

NotificationsPermissionsClient.AuthorizationOptionsUNAuthorizationOptions 类型的包装器,它的预定义值是

您还可以通过提供 UInt 原始值来构造 AuthorizationOptions 对象。

触感引擎 (HapticEngine)

HapticEngineClientHapticFeedback 客户端的工厂客户端。 HapticFeedbackUIFeedbackGenerator 的客户端。

用法

import HapticEngineClientLive

// If you need just one generator you can use HapticFeedback directly
HapticFeedback.success.trigger()

// Otherwise if you need more flexible way to create Haptic feedbacks use HapticEngineClient
HapticEngineClient.live.generator(for: .success).trigger()

UserDefaults

UserDefaultsClient 是 UserDefaults 对象的客户端,它**将对象存储为数据**(使用 DataRepresentable 协议),并为以下操作提供了接口

可以通过 rawValue: StirngStringLiteralStringInterpolation 初始化 UserDefaultsClient.Key。 此外,您可以使用 .bundle(_:Key).bundle(_:Bundle, _:Key)bundleID 前缀添加到您的密钥。

DataRepresentable

DataRepresentable 模块提供了一个用于对象数据表示的协议。 UserDefaultsClientKeychainClient 使用它来将对象存储为数据。

安装

基本

您可以通过将其作为包依赖项添加到 Xcode 项目来将 StandardClients 添加到 Xcode 项目。

  1. 文件菜单中,选择Swift Packages › Add Package Dependency…
  2. 在包存储库 URL 文本字段中输入 "https://github.com/capturecontext/swift-standard-clients.git"
  3. 选择您需要链接到您的项目的产品。

推荐

如果您的项目使用 SwiftPM,您可以将 StandardClients 添加到您的包文件中。

.package(
  name: "swift-standard-clients",
  url: "https://github.com/capturecontext/swift-standard-clients.git", 
  .upToNextMinor(from: "0.1.0")
)

不要忘记目标依赖项

.product(
  name: "SomeClientOrClientLive", 
  package: "swift-standard-clients"
)

许可证

该库是在 MIT 许可证下发布的。 有关详细信息,请参阅 LICENSE