IOS executes the specified action after being idle for a period of time (the user has no action)

Source: Internet
Author: User
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];

}

}

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.