Waitforit يجعل تنفيذ سيناريوهات تطبيق iOS شائعة نسيم:
عادةً ما يتضمن التعامل مع هذا النوع من المنطق حفظ البيانات يدويًا إلى UserDefaults ويجب إعادة إعادةتها من نقطة الصفر لكل سيناريو.
يوفر Waitforit واجهة برمجة تطبيقات إعلانية بسيطة تتيح لك التعامل مع معظم السيناريوهات الممكنة دون القلق بشأن تنفيذ الأساس.
يحتوي 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 بالباقي. عندما يتم الوفاء بجميع الشروط الخاصة بالسيناريو ، ستكون قيمة Bool التي تم تمريرها داخل كتلة tryToExecute true .
لنجرب سيناريو أكثر تعقيدًا قليلاً. تريد أن تطلب من المستخدم شراء اشتراك ، إذا قام بتثبيت تطبيق قبل أسبوع واحد على الأقل وقم بتشغيله على الأقل 5 مرات. تريد أن تسأله مرة واحدة كل يومين ولكن لا أكثر من 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() .
في Cartfile :
github "pawurb/WaitForIt" ~> 2.0.0
في Podfile الخاص بك:
platform :ios , '10.0'
use_frameworks!
target 'TargetName' do
pod 'WaitForIt'
end يستخدم LIB في الإنتاج لكنه لا يزال في مرحلة مبكرة من التطوير. اقتراحات حول كيفية تحسينها هي موضع ترحيب.