-3

SO basically all is in the title. I've searched quite a lot, but didn't find any right solution which doesn't require internet connection. If the user changes time in settings - i can't find real time since last launch.

I need that for my game, in it for every hour, even when you don't play the game, you get some coins.

If the user changes time in settings - that affect the time in NSDate() and user can cheat with coins.

serg_ov
  • 563
  • 7
  • 18
  • 1
    possible duplicate of [How long since last time app was opened in iOS?](http://stackoverflow.com/questions/4736093/how-long-since-last-time-app-was-opened-in-ios) – Robotic Cat Jun 29 '15 at 22:57
  • 1
    @RoboticCat that doesn't solve my problem. check the update – serg_ov Jun 30 '15 at 07:54

2 Answers2

3

So save the NSDate() to user defaults on app launch. The next time the app comes to the foreground, or gets launched again, get the current NSDate and subtract the saved date from it. That will give you the number of seconds between the two dates. Calculating hours from seconds is a simple matter of dividing by 3600. – Duncan C just now edit

EDIT:

Note that in newer versions of Swift (starting with Swift 2?) Most Foundation classes were defined as native Swift classes without the NS prefix. For newer versions of swift, replace all occurrences of NSDate with Date in the above.

Also note that in iOS ≥ 7.0, the Calendar has some methods that make this sort of calculation neater and easier. There's a new method dateComponents(_:from:to:) that lets you calculate the difference between 2 dates in whatever units you want. You could use that to calculate the seconds between the 2 dates more cleanly than calculating seconds, as outlined in my original answer. Calendar methods also tend to handle boundary conditions like spanning daylight savings time, leap seconds, etc.

Consider the following Swift 4/5 playground code:

import UIKit

let now = Date()
let randomSeconds = Double.random(in: 100000...3000000)
let later = now + randomSeconds

if let difference = Calendar.current.dateComponents([.second],
                                                    from: now,
                                                    to: later)
    .second {
    print(difference)
Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • If the user changes time in settings - that affect that time and user can cheat with coins. – serg_ov Jun 30 '15 at 07:49
  • 1
    Yes that's probably true. To avoid that you could require an internet connection and get the time from an internet time server. – Duncan C Jun 30 '15 at 11:13
  • However, mucking with your device's time causes all sorts of problems, so the user has to really want your game coins in order to do it. – Duncan C Jun 30 '15 at 11:14
1

Try this.

Step 1. When user exits game. Set a NSUserDefault with current time.

Step 2. When app launches, in your appDelagate file, get this value.

Step 3. Calculate diff between and award coins accordingly.

the_pantless_coder
  • 2,297
  • 1
  • 17
  • 30
  • 2
    I guess great minds think alike, regardless of the presence or absence of pants. – Duncan C Jun 29 '15 at 22:40
  • Great minds, i've added last line in the question, check that. – serg_ov Jun 30 '15 at 07:53
  • As Duncan Stated, I think the proper way would be to get the time from an internet time server. Maybe you could look into running a NSTimer in the background when the app is not active, but I am not sure if this is possible as well as the negative impact it would have on the device battery life. – the_pantless_coder Jul 01 '15 at 13:52
  • I was reading up on this, this topic should help you out. http://stackoverflow.com/questions/12221528/nsdate-get-precise-time-independent-of-device-clock – the_pantless_coder Jul 01 '15 at 14:02