Original article: http://stackoverflow.com/questions/8085188/ios-perform-action-after-period-of-inactivity-no-user-interaction
1. Create an objective-C class and inherit from uiapplication.
2. Edit. h as follows:
# Import <Foundation/Foundation. h>
// Define the application timeout time in minutes. Therefore, we multiply the value by 60 to convert the time to seconds.
# Define kapplicationtimeoutinminutes 5
// Define the notification name. The actual content is the string "timed out"
# Define kapplicationdidtimeoutnotification
@ "Apptimeout"
@ Interface timeruiapplication: uiapplication {
Nstimer * myidletimer;
}
-(Void) resetidletimer;
@ End
3. Edit. m as follows:
# Import "timeruiapplication. H"
@ Implementation timeruiapplication
// Listen to all the touch. When the screen is touched, the clock will be reset.
-(Void) sendevent :( uievent *) event {
[Super sendevent: event];
If (! Myidletimer ){
[Selfresetidletimer];
}
Nsset * alltouches = [eventalltouches];
If ([alltouches count]> 0 ){
Uitouchphase phase = (uitouch *)
[Alltouchesanyobject]). phase;
If (phase = uitouchphasebegan ){
[Self resetidletimer];
}
}
}
// Reset the clock
-(Void) resetidletimer {
If (myidletimer ){
[Myidletimerinvalidate];
}
// Convert the timeout value from minute to second
Int timeout =
Kapplicationtimeoutinminutes * 60;
Myidletimer = [nstmer
Scheduledtimerwithtimeinterval: timeout
Target: Self
Selector: @ selector (idletimerexceeded)
Userinfo: nilrepeats: No];
}
// When the timeout time is reached, the kapplicationtimeoutinminutes notification will be posted.
-(Void) idletimerexceeded {
[[Nsicationicationcenter defacenter center]
Postnotificationname:
Kapplicationdidtimeoutnotification
Object: Nil];
}
@ End
4. modify main. M:
# Import <uikit/uikit. h>
# Import "appdelegate. H"
# Import "timeruiapplication. H"
Int main (INT argc, char * argv []) {
@ Autoreleasepool {
Returnuiapplicationmain (argc, argv,
Nsstringfromclass (
[Timeruiapplicationclass]),
Nsstringfromclass (
[Appdelegate
Class]);
}
}
5. Edit appdelegate. mfile. You do not need to edit appdelegate. h.
# Import "appdelegate. H"
# Import "timeruiapplication. H"
@ Implementation appdelegate
@ Synthesize window = _ window;
-(Bool) Application :( uiapplication *) applicationdidfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {
[[Nsicationicationcenter defacenter center]
Addobserver: Self
Selector:
@ Selector (applicationdidtimeout :)
Name:
Kapplicationdidtimeoutnotification
Object: Nil];
Return yes;
}
-(Void) applicationdidtimeout :( nsnotification *) notif {
Nslog (@ "time exceeded !! ");
// This is different from the XIB file. Make sure that the ID in the following code is consistent with the storyboard identifier of the View Controller you want to jump. In this example, "mainview ". The name of my storyboard is mainstoryboard. storyboard. Make sure that your file name is consistent with the storyboardwithname parameter.
Uiviewcontroller * controller =
[[Uistoryboard
Storyboardwithname: @ "mainstoryboard"
Bundle: NULL]
Instantiateviewcontrollerwithidentifier:
@ "Mainview"];
[(Uinavigationcontroller *)
Self. Window. rootviewcontroller
Pushviewcontroller: Controller
Animated: Yes];
}
Tip: the timer is started once the touch is detected. That is to say, if you touch the main window (for example, "mainview"), even if you do not exit from the main window, the same view will still be pushed after the specified time. This is not a problem in my app, but it may be a problem for your app.
This causes the view to be pushed every X minutes. Even if the touch is detected, the clock is reset.
One solution to this problem is to declare a bool member idle in APP delegate, so that you can set it to true when you want to detect whether the user has no action, if you only want to jump to the idle view, set it to false. Then, use the following if statement in the idletimerexceeded method of timeruiapplication. Set the idle of APP delegate to true in all views that you want to detect whether the user has no action. Set idle to false for views that do not need to detect whether a user has no action.
-(Void) idletimerexceeded {
Appdelegate * appdelegate = [[uiapplication
Sharedapplication] Delegate];
If (appdelegate. idle ){
[[Nsicationicationcenter defacenter center]
Postnotificationname:
Kapplicationdidtimeoutnotification
Object: Nil];
}
}