Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7172185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:33:54+00:00 2026-05-28T15:33:54+00:00

How can I add a timer to my iOS app that is based on

  • 0

How can I add a timer to my iOS app that is based on user interaction (or lack thereof)? In other words, if there is no user interaction for 2 minutes, I want to have the app do something, in this case navigate to the initial view controller. If at 1:55 someone touches the screen, the timer resets. I would think this would need to be a global timer so no matter which view you are on, the lack of interaction starts the timer. Although, I could create a unique timer on each view. Does anyone have any suggestions, links or sample code where this has been done before?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:33:55+00:00Added an answer on May 28, 2026 at 3:33 pm

    The link that Anne provided was a great starting point, but, being the n00b that I am, it was difficult to translate into my existing project. I found a blog [original blog no longer exists] that gave a better step-by-step, but it wasn’t written for XCode 4.2 and using storyboards. Here is a write up of how I got the inactivity timer to work for my app:

    1. Create a new file -> Objective-C class -> type in a name (in my case TIMERUIApplication) and change the subclass to UIApplication. You may have to manually type this in the subclass field. You should now have the appropriate .h and .m files.

    2. Change the .h file to read as follows:

      #import <Foundation/Foundation.h>
      
      //the length of time before your application "times out". This number actually represents seconds, so we'll have to multiple it by 60 in the .m file
      #define kApplicationTimeoutInMinutes 5
      
      //the notification your AppDelegate needs to watch for in order to know that it has indeed "timed out"
      #define kApplicationDidTimeoutNotification @"AppTimeOut"
      
      @interface TIMERUIApplication : UIApplication
      {
          NSTimer     *myidleTimer;
      }
      
      -(void)resetIdleTimer;
      
      @end
      
    3. Change the .m file to read as follows:

      #import "TIMERUIApplication.h"
      
      @implementation TIMERUIApplication
      
      //here we are listening for any touch. If the screen receives touch, the timer is reset
      -(void)sendEvent:(UIEvent *)event
      {
          [super sendEvent:event];
      
          if (!myidleTimer)
          {
              [self resetIdleTimer];
          }
      
          NSSet *allTouches = [event allTouches];
          if ([allTouches count] > 0)
          {
              UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
              if (phase == UITouchPhaseBegan || phase == UITouchPhaseMoved)
              {
                  [self resetIdleTimer];
              }
      
          }
      }
      //as labeled...reset the timer
      -(void)resetIdleTimer
      {
          if (myidleTimer)
          {
              [myidleTimer invalidate];
          }
          //convert the wait period into minutes rather than seconds
          int timeout = kApplicationTimeoutInMinutes * 60;
          myidleTimer = [NSTimer scheduledTimerWithTimeInterval:timeout target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO];
      
      }
      //if the timer reaches the limit as defined in kApplicationTimeoutInMinutes, post this notification
      -(void)idleTimerExceeded
      {
          [[NSNotificationCenter defaultCenter] postNotificationName:kApplicationDidTimeoutNotification object:nil];
      }
      
      
      @end
      
    4. Go into your Supporting Files folder and alter main.m to this (different from prior versions of XCode):

      #import <UIKit/UIKit.h>
      
      #import "AppDelegate.h"
      #import "TIMERUIApplication.h"
      
      int main(int argc, char *argv[])
      {
          @autoreleasepool {
              return UIApplicationMain(argc, argv, NSStringFromClass([TIMERUIApplication class]), NSStringFromClass([AppDelegate class]));
          }
      }
      
    5. Write the remaining code in your AppDelegate.m file. I’ve left out code not pertaining to this process. There is no change to make in the .h file.

      #import "AppDelegate.h"
      #import "TIMERUIApplication.h"
      
      @implementation AppDelegate
      
      @synthesize window = _window;
      
      -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
      {      
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidTimeout:) name:kApplicationDidTimeoutNotification object:nil];
      
          return YES;
      }
      
      -(void)applicationDidTimeout:(NSNotification *) notif
      {
          NSLog (@"time exceeded!!");
      
      //This is where storyboarding vs xib files comes in. Whichever view controller you want to revert back to, on your storyboard, make sure it is given the identifier that matches the following code. In my case, "mainView". My storyboard file is called MainStoryboard.storyboard, so make sure your file name matches the storyboardWithName property.
          UIViewController *controller = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:@"mainView"];
      
          [(UINavigationController *)self.window.rootViewController pushViewController:controller animated:YES];
      }
      

    Notes: The timer will start anytime a touch is detected. This means that if the user touches the main screen (in my case “mainView”) even without navigating away from that view, the same view will push over itself after the allotted time. Not a big deal for my app, but for yours it might be. The timer will only reset once a touch is recognized. If you want to reset the timer as soon as you get back to the page you want to be at, include this code after the …pushViewController:controller animated:YES];

    [(TIMERUIApplication *)[UIApplication sharedApplication] resetIdleTimer];
    

    This will cause the view to push every x minutes if it’s just sitting there with no interaction. The timer will still reset every time it recognizes a touch, so that will still work.

    Please comment if you have suggested improvements, especially someway to disable the timer if the “mainView” is currently being displayed. I can’t seem to figure out my if statement to get it to register the current view. But I’m happy with where I’m at. Below is my initial attempt at the if statement so you can see where I was going with it.

    -(void)applicationDidTimeout:(NSNotification *) notif
    {
        NSLog (@"time exceeded!!");
        UIViewController *controller = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:@"mainView"];
    
        //I've tried a few varieties of the if statement to no avail. Always goes to else.
        if ([controller isViewLoaded]) {
            NSLog(@"Already there!");
        }
        else {
            NSLog(@"go home");
            [(UINavigationController *)self.window.rootViewController pushViewController:controller animated:YES];
            //[(TIMERUIApplication *)[UIApplication sharedApplication] resetIdleTimer];
        }
    }
    

    I am still a n00b and may have not done everything the best way. Suggestions are always welcome.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application user can add there reminder for the specific day and reminder
Ruby can add methods to the Number class and other core types to get
I know that I can add a HintPath to an external DLLs to help
Can someone tell me of a way in which I can add a user
How can I add timer to detect how much time UIButton was pressed on
I'm developing an ios app that's very basic and uses objective almost all of
Please help.... how can we add countdown timer for any product in virtuemart? and
I can add and remove the last line in my dynamic form and calculate
I know I can add a icon to the Resources.resx file of a project
I know one can add event listener for window.error. However when working with Iframes,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.