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用於生產,但仍處於開發的早期階段。歡迎有關如何改進的建議。