添加计时器到iPhone
问题描述:
我想添加一个功能,在某些推,我的计时器将启动,甚至应用程序关闭计时器将继续。可以说我的积分是10,现在计时器开始了。一旦我关闭应用程序,也许下一次,如果我打开应用程序的添加计时器到iPhone
1-如果前20分钟,它会显示在应用程序运行
如果20后2-分钟的倒计时计时器再次启动它会增加指向10.
现在什么样的计时器或类可以帮助我做到这一点?
问候
答
NSTimer
不会继续超过秒。
可以相当做到这一点:
NSString *prevTimestamp = [NSString stringWithFormat:@"%d",
[[NSDate date] timeIntervalSince1970]];
店这prevTimestamp
在NSUserDefaults
,和回来的应用程序时,
NSString *nowTimestamp = [NSString stringWithFormat:@"%d",
[[NSDate date] timeIntervalSince1970]];
时间差将是[nowTimestamp floatValue] - [prevTimestamp floatValue]
答
在您委托的应用terminiation节省时间和NSUserDefaults的,而你laucnch应用程序读取它,并继续添加时间上。即使您的应用程序在后台,其余终止应用程序
答
在你的applicationdelegate,你有这些功能:
-(void)applicationDidEnterBackground:(UIApplication *)application;
和
-(void)applicationWillEnterForeground:(UIApplication *)application;
从这些功能,你可以通过这样的跟踪时间:
-(void)applicationDidEnterBackground:(UIApplication *)application
{
backgroundTime = [[NSDate date] timeIntervalSince1970];
}
-(void)applicationWillEnterForeground:(UIApplication *)application
{
double timetoAppend = [[NSDate Date] timeIntervalSince1970] - backgroundTime];
// from here you need to add the timetoAppend to your own variable
}
同时关闭应用程序只是停止计时器..... – Venkat
像山狮说:使用NSTimeInterval设置时间戳并将其与重新进入应用程序时的新时间进行比较。 –