标准 iOS 管理器的客户端声明和实时实现
关于客户端方法的更多信息
来源 描述 Brandon Williams - 协议见证 (Protocol Witnesses) 2019 App Builders Conference 演讲 Pointfree - 协议见证 (Protocol Witnesses) Pointfree 合集 pointfree/isowords 可以在这里找到不同客户端的示例
Any
的 UserDefaults 存储是否比基于 DataRepresentable
的存储更好。CacheClient<Key, Value>
是一个基于可哈希键值对的通用客户端,它为以下操作提供了接口
saveValue(_: Value, forKey: Key)
loadValue(of: Value.Type = Value.self, forKey: Key) -> Value
removeValue(forKey: Key)
removeAllValues()
MemoryCacheClient
基于 NSCache
构建。 在底层,它使用 MemoryCache
包装器,这是 John Sundell 的 Cache 的改进版本。 您可以直接使用 MemoryCache
(它还提供了一种将自身保存到磁盘的方法,如果您的类型是可编码的)来构建您自己的 CacheClient
实现。
IDFAPermissionClient
是 ASIdentifierManager
和 ATTrackingManager
的客户端,它为以下操作提供了接口
requestAuthorizationStatus() -> AnyPublisher<AuthorizationStatus, Never>
requestAuthorization() -> AnyPublisher<AuthorizationStatus, Never>
requestIDFA() -> AnyPublisher<UUID?, Never>
IDFAPermissionClient.AuthorizationStatus
是ATTrackingManager.AuthorizationStatus
类型和ASIdentifierManager.isAdvertisingTrackingEnabled
值的包装器,它的值是
notDetermined = "Not Determined"
// ATTrackingManager.AuthorizationStatus.notDetermined
restricted = "Restricted"
// ATTrackingManager.AuthorizationStatus.restricted
denied = "Denied"
// ATTrackingManager.AuthorizationStatus.denied
authorized = "Authorized"
// ATTrackingManager.AuthorizationStatus.authorized
unknown = "Unknown"
// ATTrackingManager.AuthorizationStatus.unknown
unavailableWithTrackingEnabled = "Unavailable: Tracking Enabled"
// iOS<14 macOS<11, tvOS<14 ASIdentifierManager.shared().isAdvertisingTrackingEnabled == true
unavailableWithTrackingDisabled = "Unavailable: Tracking Disabled"
// iOS<14 macOS<11, tvOS<14 ASIdentifierManager.shared().isAdvertisingTrackingEnabled == false
它还有一个计算属性
isPermissive
,对于.authorized
和.unavailableWithTrackingEnabled
它是true
KeychainClient
是用于安全框架钥匙串访问的客户端,它**将对象存储为数据**(使用 DataRepresentable
协议),并为以下操作提供了接口
saveValue<Value: DataRepresentable>(_: Value, forKey: Key, policy: AccessPolicy)
loadValue<Value: DataRepresentable>(of: Value.Type = Value.self, forKey: Key) -> Value
removeValue(forKey: Key)
可以通过 rawValue: Stirng
、StringLiteral
或 StringInterpolation
初始化 KeychainClient.Key
。 此外,您可以使用 .bundle(_:Key)
或 .bundle(_:Bundle, _:Key)
将 bundleID
前缀添加到您的密钥。
KeychainClient.Operations.Save.AccessPolicy
是 kSec 访问常量的包装器,它的值是
accessibleWhenUnlocked
// kSecAttrAccessibleWhenUnlocked
accessibleWhenUnlockedThisDeviceOnly
// kSecAttrAccessibleWhenUnlockedThisDeviceOnly
accessibleAfterFirstUnlock
// kSecAttrAccessibleAfterFirstUnlock
accessibleAfterFirstUnlockThisDeviceOnly
// kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
accessibleWhenPasscodeSetThisDeviceOnly
// kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly
accessibleAlways
// kSecAttrAccessibleAlways
accessibleAlwaysThisDeviceOnly
// kSecAttrAccessibleAlwaysThisDeviceOnly
NotificationsPermissionsClient
是 UNUserNotificationCenter
的客户端,它为以下操作提供了接口
requestAuthorizationStatus() -> AnyPublisher<AuthorizationStatus, Never>
requestAuthorization(options: AuthorizationOptions) -> AnyPublisher<AuthorizationStatus, Never>
configureRemoteNotifications(_:)
// 将 .register
或 .unregister
传递给该函数
NotificationsPermissionsClient.AuthorizationStatus
是UNAuthorizationStatus
类型的包装器,它的值是
notDetermined
denied
authorized
provisional
ephemeral
// 仅限 iOS14+
它还有一个计算属性
isPermissive
,对于authorized
、ephimeral
和provisional
它是 true
NotificationsPermissionsClient.AuthorizationOptions
是UNAuthorizationOptions
类型的包装器,它的预定义值是
badge
sound
alert
carPlay
criticalAlert
providesAppNotificationSettings
provisional
announcement
// 仅限 iOS
您还可以通过提供
UInt
原始值来构造AuthorizationOptions
对象。
HapticEngineClient
是 HapticFeedback
客户端的工厂客户端。 HapticFeedback
是 UIFeedbackGenerator
的客户端。
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()
UserDefaultsClient
是 UserDefaults 对象的客户端,它**将对象存储为数据**(使用 DataRepresentable
协议),并为以下操作提供了接口
saveValue<Value: DataRepresentable>(_: Value, forKey: Key)
loadValue<Value: DataRepresentable>(of: Value.Type = Value.self, forKey: Key) -> Value
removeValue(forKey: Key)
可以通过 rawValue: Stirng
、StringLiteral
或 StringInterpolation
初始化 UserDefaultsClient.Key
。 此外,您可以使用 .bundle(_:Key)
或 .bundle(_:Bundle, _:Key)
将 bundleID
前缀添加到您的密钥。
DataRepresentable
模块提供了一个用于对象数据表示的协议。 UserDefaultsClient
和 KeychainClient
使用它来将对象存储为数据。
您可以通过将其作为包依赖项添加到 Xcode 项目来将 StandardClients 添加到 Xcode 项目。
"https://github.com/capturecontext/swift-standard-clients.git"
如果您的项目使用 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。