I'm trying to create a timeout function for an app I'm develop using Swift 2 but in swift 2, you can put this code in the app delegate and it works but it does not detect any keyboard presses, button presses, textfield presses, and 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) {
//Stuff
timeoutModel.actionPerformed();
}
}
}
Before swift 2, I was able to have the AppDelegate subclass UIApplication and override sendEvent: like this:
-(void)sendEvent:(UIEvent *)event
{
[super sendEvent:event];
// Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
NSSet *allTouches = [event allTouches];
if ([allTouches count] > 0) {
// allTouches count only ever seems to be 1, so anyObject works here.
UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
[[InactivityModel instance] actionPerformed];
}
}
The code above works for every touch but the swift equivalent only works when a view does not exist above that UIWindow's hierarchy?
Does anyone know a way to detect every touch in the application?
UIWindow
also has asendEvent
method that you can override. That would allow you to track the time since the last screen touch. Swift 4:If you're using a storyboard, you can load it in
didFinishLaunchingWithOptions
:Now elsewhere in your app, you can check for inactivity like this:
As I have something similar in my application, I just tried to fix it:
sendEvent
inUIWindow
- doesn't worksendEvent
in delegate - doesn't workSo the only way is to provide custom
UIApplication
subclass. My code so far (works on iOS 9) is:Why there's
@objc(MyApplication)
. That's because Swift mangles names in a different way then Objective-C and it just says - my class name in Objective-C isMyApplication
.To make it working, open your info.plist and add row with Principal class key and
MyApplication
value (MyApplication
is what's inside@objc(...)
, not your Swift class name). Raw key isNSPrincipalClass
.