Adam Meadows
Adam Meadows

Reputation: 73

iOS timer outside application

I am trying to make my first app, its going really well. I know quite a few programming languages, however I've run into a problem.

I am trying to limit the user to do, at most, one action every hour. I've been trying to make a timer. I've also dabbled in some server side code to help me, however I can't seem to make it work. I was wondering if anyone had any answers that could help me?

Upvotes: 2

Views: 167

Answers (4)

Tomasz Zabłocki
Tomasz Zabłocki

Reputation: 1326

Try something like this:

@interface YourClass
@property (assign, nonatomic) double lastActionTime;
@end

@implementation YourClass

- (BOOL)isActionAllowed
{
    double currentTime = [[NSDate date] timeIntervalSince1970];
    if (currentTime - 3600 > lastActionTime)
    {
        lastActionTime = currentTime;
        return YES;
    }
    return NO;
}

@end

You may save lastActionTime in NSUserDefaults to keep it even app is closed.

That solution assuming user do not change date or re-install app.

Upvotes: 0

Apurv
Apurv

Reputation: 17186

You can't achieve it through iPhone locally.

  • One can change the clock time on device and solution with stored time stamp will not work.
  • Timers are not running in back ground. So that solution will not work.

To achieve it, you will have to write server side business. Before performing any operation, check from server side, if an hour has been passed or not. Whenever there will be a request to server, server will update last operation time(if hour has been passed) for that particular user. This time will be set by server and not dependent on client's local time.

Upvotes: 1

Shamsudheen TK
Shamsudheen TK

Reputation: 31311

Use NSTimer to disable the action an hour.

scheduledTimerWithTimeInterval will help you to achieve this.

NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:yourTimeinterval target:self selector:@selector(trigger) userInfo:nil repeats:YES]; 

Upvotes: 0

Anton
Anton

Reputation: 2483

Probably the best approach would be the following: 1. Keep the last time of user action is some file or a database (or in NSUserDefaults) 2. Upon letting the user perform an action check the stored timestamp to see if 1 hour has passed since last invocation.

Additionally, it might be neccessary to check this against a server-side (to avoid removing and re-installing the application reset the stored timestamp).

Upvotes: 0

Related Questions