Pseudo code:
- Have a timer running that invokes a method after time is up(session time-out period).
- The method should navigate to the Login activity
- Reset the timer if there is any user interaction with the app.
Code:
1) Create a new file -> Objective-C class -> type in a name (in my case SessionApplication)
2) 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.
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 SessionTimeoutPeriodMins 5
//the notification your AppDelegate needs to watch for in order to know that it has indeed "timed out"
#define kapplicationDidSessionTimeoutNotification @"AppTimeOut"
@interface SessionApplication : UIApplication
{
NSTimer *mySessionTimer;
}
-(void)resetSessionTimer;
@end
Change the .m file to read as follows:
#import "SessionApplication.h"
@implementation SessionApplication
//here we are listening for any touch. If the screen receives touch, the timer is reset
-(void)sendEvent:(UIEvent *)event
{
[super sendEvent:event];
if (!mySessionTimer)
{
[self resetSessionTimer];
}
NSSet *allTouches = [event allTouches];
if ([allTouches count] > 0)
{
UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
if (phase == UITouchPhaseBegan)
{
[self resetSessionTimer];
}
}
}
//as labeled...reset the timer
-(void)resetSessionTimer
{
if (mySessionTimer)
{
[mySessionTimer invalidate];
}
//convert the wait period into minutes rather than seconds
int timeout = SessionTimeoutPeriodMins * 60;
mySessionTimer = [NSTimer scheduledTimerWithTimeInterval:timeout target:self selector:@selector(sessionTimedOut) userInfo:nil repeats:NO];
}
//if the timer reaches the limit as defined in SessionTimeoutPeriodMins, post this notification
-(void)sessionTimedOut
{
[[NSNotificationCenter defaultCenter] postNotificationName:kapplicationDidSessionTimeoutNotification object:nil];
}
@end
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 "SessionApplication.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, NSStringFromClass([SessionApplication class]), NSStringFromClass([AppDelegate class]));
}
}
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 "SessionApplication.h"
@implementation AppDelegate
@synthesize window = _window;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidSessionTimeout:) name:kapplicationDidSessionTimeoutNotification object:nil];
return YES;
}
-(void)applicationDidSessionTimeout:(NSNotification *) notif
{
NSLog (@“session timed out!!");
//Get the controller to login activity
}
Reference
Sample