[英]ios background task timer
I'm designing an app requiring sending data to server at a certain time in background mode. 我正在设计一个需要在特定时间在后台模式下将数据发送到服务器的应用程序。 So I'm wondering: Is it possible for an app to execute a piece of code 24h after I scheduled it(just once)?
所以我想知道:在我安排一次应用程序之后(一次),应用程序是否有可能在24小时内执行一段代码?
Thanks a lot! 非常感谢!
store date in NSUserDefaults
and further you check for 24 hours .. 将日期存储在
NSUserDefaults
然后进一步检查24小时..
NSDate *date = [[NSUserDefaults standardUserDefaults] objectForKey:@"CONSCIENCE_START_DATE"];
if (!date) {
// This is the 1st run of the app
date = [NSDate date];
[[NSUserDefaults standardUserDefaults] setObject:date forKey:@"CONSCIENCE_START_DATE"];
}
NSString *str = [NSString stringWithFormat:@"%@",date];
NSLog(@" date string = %@",str);
NSTimeInterval secondsBetween = [[NSDate date] timeIntervalSinceDate:date]; // in seconds
int numberOfHours = secondsBetween / 3600; // convert in hours
NSLog(@"There are %d hours in between the two dates.", numberOfHours);
if (numberOfHours > 24) {
// do your code here ...
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"CONSCIENCE_START_DATE"];
}
hope it help you . 希望对你有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.