public
Last active

  • Download Gist
KSDIdlingWindow.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
//
// KSDIdlingWindow.h
//
// Created by Brian King on 4/13/10.
// Copyright 2010 King Software Designs. All rights reserved.
//
// Based off:
// http://stackoverflow.com/questions/273450/iphone-detecting-user-inactivity-idle-time-since-last-screen-touch
//
 
#import <UIKit/UIKit.h>
 
extern NSString * const KSDIdlingWindowIdleNotification;
extern NSString * const KSDIdlingWindowActiveNotification;
 
@interface KSDIdlingWindow : UIWindow {
NSTimer *idleTimer;
NSTimeInterval idleTimeInterval;
}
 
@property (assign) NSTimeInterval idleTimeInterval;
 
@property (nonatomic, retain) NSTimer *idleTimer;
 
@end
KSDIdlingWindow.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
//
// KSDIdlingWindow.m
//
// Created by Brian King on 4/13/10.
// Copyright 2010 King Software Designs. All rights reserved.
//
// Based off:
// http://stackoverflow.com/questions/273450/iphone-detecting-user-inactivity-idle-time-since-last-screen-touch
//
 
#import "KSDIdlingWindow.h"
 
NSString * const KSDIdlingWindowIdleNotification = @"KSDIdlingWindowIdleNotification";
NSString * const KSDIdlingWindowActiveNotification = @"KSDIdlingWindowActiveNotification";
 
@interface KSDIdlingWindow (PrivateMethods)
- (void)windowIdleNotification;
- (void)windowActiveNotification;
 
 
@end
 
 
@implementation KSDIdlingWindow
@synthesize idleTimer, idleTimeInterval;
 
- (id) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.idleTimeInterval = 0;
}
return self;
}
#pragma mark activity timer
 
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
NSSet *allTouches = [event allTouches];
if ([allTouches count] > 0) {
 
// To reduce timer resets only reset the timer on a Began or Ended touch.
UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded) {
if (!idleTimer) {
[self windowActiveNotification];
} else {
[idleTimer invalidate];
}
if (idleTimeInterval != 0) {
self.idleTimer = [NSTimer scheduledTimerWithTimeInterval:idleTimeInterval
target:self
selector:@selector(windowIdleNotification)
userInfo:nil repeats:NO];
}
}
}
}
 
 
- (void)windowIdleNotification {
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc postNotificationName:KSDIdlingWindowIdleNotification
object:self
userInfo:nil];
self.idleTimer = nil;
}
 
- (void)windowActiveNotification {
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc postNotificationName:KSDIdlingWindowActiveNotification
object:self
userInfo:nil];
}
 
- (void)dealloc {
if (self.idleTimer) {
[self.idleTimer invalidate];
self.idleTimer = nil;
}
[super dealloc];
}
 
 
@end

Thanks for the code snippet - definitely made it easy to see where I needed to start.

Thanks for the snippet - very helpful!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.