测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可 | MIT |
发布最新发布 | 2017年10月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Pawel Urbanek 维护。
WaitForIt 让实现常见的 iOS 应用场景变得轻而易举
处理此类逻辑通常需要手动将数据保存到 UserDefaults
,并且对于每个场景都需要从头开始。
WaitForIt 提供了一个简单的声明式 API,允许您处理大多数可能的场景,无需担心底层实现。
ScenarioProtocol
拥有以下属性,可以用来定义何时执行一个场景
protocol ScenarioProtocol {
// minimum number of scenario events needed to be trigerred before scenario can be executed
static var minEventsRequired: Int? { get set }
// maximum number of scenario events which can be trigerred before scenario stops executing
static var maxEventsPermitted: Int? { get set }
// maximum number of times that scenario can be executed
static var maxExecutionsPermitted: Int? { get set }
// minimum time interval, after the first scenario event was trigerred, before the scenario can be executed
static var minSecondsSinceFirstEvent: TimeInterval? { get set }
// minimum time interval, after the last scenario event was trigerred, before the scenario can be executed
static var minSecondsSinceLastEvent: TimeInterval? { get set }
// minimum time interval before scenario can be executed again after previous execution
static var minSecondsBetweenExecutions: TimeInterval? { get set }
// custom conditions closure
static var customConditions: (() -> Bool)? { get set }
}
场景是一个简单的结构体,它实现了一个单一的 config
函数。您使用它来配置指定场景何时执行的值。
您可以使用静态方法操作场景结构体
// increment scenario specific event counter
static func triggerEvent()
// try to execute a scenario (it counts as executed only if bool param passed into a block was `true`)
static func tryToExecute(completion: @escaping (Bool) -> Void)
// reset scenario event and execution counters
static func reset()
假设您只想显示一次教程屏幕
import WaitForIt
struct ShowTutorial: ScenarioProtocol {
static func config() {
maxExecutionsPermitted = 1
}
}
// In ViewController.swift
func viewDidLoad() {
super.viewDidLoad()
ShowTutorial.tryToExecute { didExecute in
if didExecute {
self.showTutorial()
}
}
}
就是这样!您不再需要自己处理 UserDefaults
。只需配置一个具有正确执行条件的结构体,库就会处理其余部分。当场景的所有条件都满足时,在 tryToExecute
块内传入的布尔值将是 true
。
让我们尝试一个更复杂的场景。您想询问用户是否购买订阅,如果他在至少一周前安装了应用并至少打开了 5 次。您想每隔 2 天询问一次,但总共不超过 4 次
import WaitForIt
struct AskToSubscribe: ScenarioProtocol {
static func config() {
minEventsRequired = 5
minSecondsSinceFirstEvent = 604 800 // seconds in one week
maxExecutionsPermitted = 4
minSecondsBetweenExecutions = 172 800 // seconds in two days
}
}
// In AppDelegate.swift
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
AskToSubscribe.triggerEvent()
AskToSubscribe.tryToExecute { didExecute in
if didExecute {
self.askToSubscribe()
}
}
return true
}
如果您场景中时间计数和事件信息的条件不足以满足您的需求,您还可以定义一个自定义条件闭包。该闭包将每次尝试执行场景时进行评估
struct ShowLowBrightnessAlertOnce: ScenarioProtocol {
static func config() {
customConditions = {
return UIScreen.main.brightness < 0.3
}
maxExecutionsPermitted = 1
}
}
如果决定混合多个场景结构体中的条件,可以实现更复杂的故事。当然,您也可以将事件触发和场景执行分散到应用中,它们不需要在同一文件中。
实现基于标准 UserDefaults
,所以如果应用程序被重新安装,数据将不会持久化。UserDefaults
的键名由结构体名称生成,因此重命名结构体会重置其所有数据。您还可以使用 reset()
方法重置持久化数据。
库在生产中使用,但它仍然处于开发的早期阶段。欢迎提出如何改进的建议。