iOS perform action after period of inactivity (no user interaction)

前端 未结 6 1995
慢半拍i
慢半拍i 2020-11-28 01:46

How can I add a timer to my iOS app that is based on user interaction (or lack thereof)? In other words, if there is no user interaction for 2 minutes, I want to have the ap

6条回答
  •  离开以前
    2020-11-28 02:08

    Notes: The timer will start anytime a touch is detected. This means that if the user touches the main screen (in my case "mainView") even without navigating away from that view, the same view will push over itself after the allotted time. Not a big deal for my app, but for yours it might be. The timer will only reset once a touch is recognized. If you want to reset the timer as soon as you get back to the page you want to be at, include this code after the ...pushViewController:controller animated:YES];

    One solution to this problem of the same view beginning displayed again is to have a BOOL in the appdelegate and set this to true when you want to check for the user being idle and setting this to false when you have moved to the idle view. Then in the TIMERUIApplication in the idleTimerExceeded method have an if statement as below. In the viewDidload view of all the views where you want to check for the user beginning idle you set the appdelegate.idle to true, if there are other views where you do not need to check for the user being idle you can set this to false.

    -(void)idleTimerExceeded{
              AppDelegate *appdelegate = [[UIApplication sharedApplication] delegate];
    
              if(appdelegate.idle){
                [[NSNotificationCenter defaultCenter] postNotificationName: kApplicationDidTimeOutNotification object:nil]; 
              }
    }
    

提交回复
热议问题