如何让 iPhone 应用程序限制每天使用一次

发布于 2024-11-02 03:27:27 字数 341 浏览 0 评论 0原文

所以基本上,我一直在尝试我在这里和其他地方读过的很多东西,但一直没能成功。

我一直在使用 setObject 等将 NSDate 保存为 UserDefaults 中的对象。然后使用 [NSData date] 等获取当前时间,我的问题是比较。我看过其他人的例子,但似乎没有什么能满足我的需要。

这只是一个 if 语句,我需要它知道自上次执行此操作以来已经过了 18 小时(并且它保存了 NSDate),才能对变量进行 ++ ++ 操作。否则它只显示一条消息。我遇到的问题是我不知道如何检查当前日期是否>保存日期 18 小时以上。有人可以帮忙吗?我尝试将它们转换为 NSTimeIntervals 并从中计算,但不知何故我搞砸了。

So basically, i've been trying a lot of the stuff i've read on here and elsewhere, and haven't been able to get this working.

I've been saving an NSDate as an object in UserDefaults with setObject etc. Then get current time with [NSData date] etc, my problem is comparison. I've seen other peoples examples and nothing seems to do what I need.

It's just an if statement and I need it to know to ++ the variable if 18 hours have passed from the last time they did it (and it saved the NSDate). Else it just displays a msg. The problem i'm having is I cant figure out how to check if current date > saved date by 18hours +. Could someone please help? I've tried turning them into NSTimeIntervals and calculating from that but somehow I screw this up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的旅程 2024-11-09 03:27:27

我会在该应用的 NSUserDefaults 中存储 LastAccessDate 。在应用程序打开、启动、活动等时,我会使用 timeIntervalSinceNow 检查该值是否小于 18 小时。查看 NSDate 类< /a>.

示例(未测试):

NSDate *lastAccessDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastAccessDate"]]
NSTimeInterval duration = [lastAccessDate timeIntervalSinceNow];
int hoursPassed = duration / 3600;

I'd store a LastAccessDate in NSUserDefaults for that app. Upon app open, launch, active, etc I'd check to see that this value was less than 18 hours with timeIntervalSinceNow. Check out the NSDate Class.

Example (not tested):

NSDate *lastAccessDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastAccessDate"]]
NSTimeInterval duration = [lastAccessDate timeIntervalSinceNow];
int hoursPassed = duration / 3600;
滿滿的愛 2024-11-09 03:27:27

限制每天使用一次是什么意思?您的意思是让应用程序每天只做一次有用的事情?当然,这是一条通往应用商店拒绝的道路,特别是如果你在应用程序加载后根本不让用户做任何事情的话。

无论如何,如果你想继续做这样的事情,逻辑可能是这样的:

NSDate *lastSyncDate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"last_app_rundate"];  
NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:lastSyncDate];

if ((interval / 3600) >= 18) {
   // let your user use your app
   [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"last_app_rundate"];
}

编辑:回答太慢了!

What do you mean by limit use to once a day? You mean only let the app do something useful once a day? Surely this is a road to App Store rejection, especially if you don't let the user do anything at all once the app is loaded.

In any event, if you want to proceed with such things, the logic is probably something like this:

NSDate *lastSyncDate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"last_app_rundate"];  
NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:lastSyncDate];

if ((interval / 3600) >= 18) {
   // let your user use your app
   [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"last_app_rundate"];
}

edit: too slow to answer!

爱的十字路口 2024-11-09 03:27:27

试试这个,

NSTimeInterval dateDiff = [lastAccessDate timeIntervalSinceNow];
int hours=trunc(dateDiff/(60*60));

Try this,

NSTimeInterval dateDiff = [lastAccessDate timeIntervalSinceNow];
int hours=trunc(dateDiff/(60*60));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文