I am trying to create a timeout function for an application that I am developing using Swift 2, but in swift 2 you can put this code in the application delegate and it works, but it does not detect keystrokes, button presses, text presses, etc .:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event);
let allTouches = event!.allTouches();
if(allTouches?.count > 0) {
let phase = (allTouches!.first as UITouch!).phase;
if(phase == UITouchPhase.Began || phase == UITouchPhase.Ended) {
timeoutModel.actionPerformed();
}
}
}
Before Swift 2, I was able to subclass AppDelegate UIApplication and override sendEvent: for example:
-(void)sendEvent:(UIEvent *)event
{
[super sendEvent:event];
NSSet *allTouches = [event allTouches];
if ([allTouches count] > 0) {
UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
[[InactivityModel instance] actionPerformed];
}
}
The code above works for every touch, but the quick equivalent only works when the view does not exist on this UIWindow hierarchy?
Does anyone know a way to detect every touch in the app?
source
share