现在我需要让用户打开APP的时间,并将时间与上次比较,如果时间超过24小时,只是为了提醒用户更新App。谁知道关于这个的Objective-C代码?请帮帮我。非常感谢你!
答案 0 :(得分:4)
在AppDelegate文件中创建一个NSDate成员变量(即NSDate * startDate)。现在在AppDelegate.m类中使用以下代码。
- (void)applicationDidEnterBackground:(UIApplication *)application {
// save the last app opening time, you can save in NSuserdefaults also
startDate = [NSDate date];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSDate *now = [NSDate date];
// Returns in seconds
NSTimeInterval timeInterval = [now timeIntervalSinceDate:startDate]
if(timeInterval > (24*3600)) {
// remind the user of updating App here.
}
}
答案 1 :(得分:3)
我建议将值存储在用户默认值中,这样如果用户退出应用程序,我们就可以获得数据时间详细信息。在app delegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDate *dateTimenow=[NSDate date];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm"];
NSString *strDateTimeNow=[NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:dateTimenow]];
NSLog(@"now date time: %@",strDateTimeNow);
NSUserDefaults *userdefaults=[NSUserDefaults standardUserDefaults];
if ([userdefaults valueForKey:@"LastAppLaunchTime"]) {
NSString *strPreviousDateTime=[NSString stringWithFormat:@"%@",[userdefaults valueForKey:@"LastAppLaunchTime"]];
NSLog(@"previous date time: %@",strPreviousDateTime);
//Now you can compare current date time with previous date time
//perform task after comparing
}
[userdefaults setValue:strDateTimeNow forKey:@"LastAppLaunchTime"];// saving recent date time details for next time
你也可以在后台进行前景时这样做,所以如果app在后台暂停超过24小时,那么你也可以执行你的任务。我希望这会有所帮助。
答案 2 :(得分:0)
您可以使用NSDate:
NSDate* now = [NSDate date];
将此值保存为lastTimeOpened并在下次启动时进行比较:
if ([now timeIntervalSinceDate:lastTimeOpened] > someTime)