在指定时间段iPhone后清除NSUserDefaults [英] Clearing NSUserDefaults after specified time period iPhone

查看:144
本文介绍了在指定时间段iPhone后清除NSUserDefaults的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了NSUserDefaults对象,每当偶数发生时,它将被更新为新值。
我想要的是(根据我的应用程序要求)该对象应该每7天清除一次。
像第一次NSUserDefaults今天更新,所以7天后,一个方法应该工作,并清除NSUserDefaults。

I have created NSUserDefaults object, it will be updated with the new value whenever an even will occur. What I want is (as per my application requirement) that object should be cleared every 7 days. Like if 1st time that NSUserDefaults are updated today so exact after 7 days, a method should work and clear the NSUserDefaults. So that, new values would be assigned from then on for the next 7 days.

推荐答案

你可以做的是存储一个NSDate对象。然后每次启动应用程序(或更频繁地)检查当时和现在之间的时间差是否为7天。

What you can do is store an NSDate object. Then every time the application is launched (or more often) check if the time difference between then and now is 7 days.

const NSString *kFirstLaunchDateKey = @"firstLaunchDate";
NSDate *firstLaunchDate = [[NSUserDefaults standardUserDefaults] objectForKey:kFirstLaunchDateKey];

//  If this is the first time the app has been launched we record right now as the first time the app was launched.
if (!firstLaunchDate) {
    [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:kFirstLaunchDateKey];
    return;
}

NSTimeInterval *diff = abs([firstLaunchDate timeIntervalSinceNow]);
if (diff > 60 * 60 * 24 * 7) {
//      Seven days have passed since the app was first launched. 
//      Display the rate button.
}

如果是

- (void)removePersistentDomainForName:(NSString *)domainName

以您的应用程序包标识符作为参数。

With your application’s bundle identifier as the parameter.

从Apple的文档:


domainName
需要其键和值的域。此值应等于应用程序的包标识符。

domainName The domain whose keys and values you want. This value should be equal to your application's bundle identifier.

这篇关于在指定时间段iPhone后清除NSUserDefaults的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆