Reputation: 344
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
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
if currentdate is earlier then saved currentdate then you have to take coins because that is invalid coins
how about my idea?
Upvotes: 1
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