The link provided by Anne was a great starting point, but being n00b, which I am, was difficult to translate into my existing project. I found a blog [the original blog no longer exists] that gave the best step by step, but it was not written for Xcode 4.2 and using storyboards. Here is a record of how I got an inactivity timer to work in my application:
Create a new file -> Objective-C class -> enter a name (in my case TIMERUIApplication) and change the subclass to UIApplication. You may need to manually enter this in the subclass field. You should now have the corresponding .h and .m files.
Modify the .h file as follows:
Modify the .m file to read as follows:
Go to the Supporting Files folder and change main.m to this (unlike previous 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])); } }
Write the remaining code in the AppDelegate.m file. I left code not related to this process. There are no changes to 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!!");
Notes. The timer starts at any time when a touch is detected. This means that if the user touches the main screen (in my case "mainView"), without even moving from this view, the same view will click on itself after the allotted time. Not very important for my application, but it can be for you. The timer will only reset after touch recognition. If you want to reset the timer, as soon as you return to the page you want to be on, include this code after ... pushViewController: controller animated: YES];
[(TIMERUIApplication *)[UIApplication sharedApplication] resetIdleTimer];
This will make the view click every x minutes if it just sits there without interaction. The timer will reset every time it recognizes a touch, so that it will still work.
Please comment if you suggested improvements, especially sometime, to turn off the timer if "mainView" is currently displayed. It seems I can not understand what the if statement is so that it registers the current view. But I am pleased with where I am. Below is my initial attempt at an if statement so that you can see where I'm heading.
-(void)applicationDidTimeout:(NSNotification *) notif { NSLog (@"time exceeded!!"); UIViewController *controller = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:@"mainView"];
I am still n00b and may not have done all the best. Suggestions are always welcome.
BobbyScon Jan 24 2018-12-12T00: 00Z
source share