LodgeApps
LodgeApps

Reputation: 344

NSTimer After User Leaves App

I am creating an iOS app in Objective C. I don't want to pay for a server, but I want to create a reward system so that every 3 hours the user is rewarded with "coins" when they enter the app. For example, if a user exits the app at 1pm and re-enters the app at 5pm, a reward will be ready. However I want this to carry over even if the app is not running in the background.

Is this possible?

Upvotes: 0

Views: 80

Answers (2)

Cruz
Cruz

Reputation: 2632

In your AppDelegate , you can save exit date time to NSUserDefaults

when below function did called

func applicationWillTerminate(application: UIApplication)

func applicationDidEnterBackground(application: UIApplication)

when user enter foreground, you can compare exit time with current time and give some coins

but this way have some problem

if user edited iPhone's date or time in setting , can get a log of coins

but if you use server , you can compare date correctly

there are some idea to solve this problem without server

how about save current time or date periodically ?

this is usecase

  1. user edit iphone's time to 3 hour after
  2. user enter app
  3. user get coins
  4. user edit iphone's time to 3 hour before
  5. user lose coins and some item

if currentdate is earlier then saved currentdate then you have to take coins because that is invalid coins

how about my idea?

Upvotes: 1

Chenglu
Chenglu

Reputation: 884

Maybe not a timer here, but save users' exit time through NSUserDefaults, and then whenever users re-enter the app, compare the exit time with the current time to see if the gap is 3 hours.

For example, in viewWillDisappear() You might save the exit time through:

NSUserDefaults.standardUserDefaults().setObject(NSDate(), forKey: "exitTime")

And in viewDidLoad(), you could extract the exit time through

NSUserDefaults.standardUserDefaults().objectForKey("exitTime")

Upvotes: 2

Related Questions