这是一个轻量级库,用于在应用程序生命周期或版本生命周期中仅一次执行Objective-C代码。例如,仅在应用程序第一次运行时执行代码/块。
我们建议您使用CocoaPods安装此项目
platform :ios, '6.0'
pod "YCFirstTime"
使用此选项,例如,运行一个欢迎对话框或创建一个初始数据库。
[[YCFirstTime shared] executeOnce:^{
/// Some code that should run only ONCE per app installation.
} forKey:@"CHOOSE_AN_UNIQUE_KEY_FOR_THIS_SNIPPET"];
您可以利用此功能向用户展示新版本的新功能。
[[YCFirstTime shared] executeOncePerVersion:^{
/// Some code that should run only ONCE per app version.
/// This code will run in your version 1.0 and as well in the 1.1
} forKey:@"CHOOSE_AN_UNIQUE_KEY_FOR_THIS_SNIPPET"];
用例5:每天每小时每分钟每秒请求一次其他内容。天数参数为 CGFloat,根据需要使用。
[[YCFirstTime shared] executeOncePerInterval:^{
/// Some code that should run only ONCE per app version.
/// This code will run in your version 1.0 and as well in the 1.1
} forKey:@"CHOOSE_AN_UNIQUE_KEY_FOR_THIS_SNIPPET" withDaysInterval:2.0f];
这对于需要第一次突出某些元素但从此之后希望执行另一个代码非常有用。
[[YCFirstTime shared] executeOnce:^{
/// Some code that should run only ONCE per app installation.
} executeAfterFirstTime:^{
/// Another piece of code to run from the SECOND time on.
} forKey:@"CHOOSE_AN_UNIQUE_KEY_FOR_THIS_SNIPPET"];
[[YCFirstTime shared] executeOncePerVersion:^{
/// Some code that should run only ONCE per app installation.
} executeAfterFirstTime:^{
/// Another piece of code to run from the SECOND time on.
} forKey:@"CHOOSE_AN_UNIQUE_KEY_FOR_THIS_SNIPPET"];
如果您想删除所有以前的执行并从零开始,可以调用
[[YCFirstTime shared] reset];
在iOS6+上运行正常,需要ARC。