Skip to content

Instantly share code, notes, and snippets.

@KingOfBrian
Created April 14, 2010 16:10
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save KingOfBrian/365998 to your computer and use it in GitHub Desktop.
Save KingOfBrian/365998 to your computer and use it in GitHub Desktop.
//
// 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
//
// 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
@astralbodies
Copy link

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

@raunakp
Copy link

raunakp commented Nov 11, 2013

Thanks for the snippet - very helpful!

@swalehapatanwala
Copy link

But how can we use it further? I have set my rootviewcontrollers to this customwindow object. But it doesn't give any results?

@milanivarni
Copy link

milanivarni commented Jan 12, 2017

How to use this snippets in particular view controller, in Swift 3?

@agarcian
Copy link

agarcian commented Jan 2, 2018

In the app delegate add the following:

{
    static KSDIdlingWindow *customWindow = nil;
    if (!customWindow) customWindow = [[KSDIdlingWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    return customWindow;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment