WaitForIt
1.0.0
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 。只需配置有正确的执行条件的结构,LIB就会照顾其余的。当满足您方案的所有条件时, tryToExecute块内传递的BOOL值将是true 。
让我们尝试一些更复杂的场景。您想要求用户购买订阅,如果他至少在1周前安装了一个应用程序并将其打开至少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密钥名称使用结构名称生成,因此重命名struct将重置其所有数据。您还可以使用reset()方法重置持久数据。
在您的Cartfile中:
github "pawurb/WaitForIt" ~> 2.0.0
在您的Podfile中:
platform :ios , '10.0'
use_frameworks!
target 'TargetName' do
pod 'WaitForIt'
end LIB用于生产,但仍处于开发的早期阶段。欢迎有关如何改进的建议。