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는 생산에 사용되지만 여전히 개발 초기 단계에 있습니다. 개선 방법에 대한 제안을 환영합니다.