如何让 iPhone 应用程序限制每天使用一次
所以基本上,我一直在尝试我在这里和其他地方读过的很多东西,但一直没能成功。
我一直在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会在该应用的 NSUserDefaults 中存储 LastAccessDate 。在应用程序打开、启动、活动等时,我会使用
timeIntervalSinceNow
检查该值是否小于 18 小时。查看 NSDate 类< /a>.示例(未测试):
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 withtimeIntervalSinceNow
. Check out the NSDate Class.Example (not tested):
限制每天使用一次是什么意思?您的意思是让应用程序每天只做一次有用的事情?当然,这是一条通往应用商店拒绝的道路,特别是如果你在应用程序加载后根本不让用户做任何事情的话。
无论如何,如果你想继续做这样的事情,逻辑可能是这样的:
编辑:回答太慢了!
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:
edit: too slow to answer!
试试这个,
Try this,