简体   繁体   English

使用NSTimer注销应用程序

[英]Logout application using NSTimer

I have a Login page that segues to the rest of the application. 我有一个登录页面,可以连接到该应用程序的其余部分。 In the rest of my application the remaining view controllers extend from my own defined 'BaseViewController`. 在我的应用程序的其余部分中,其余的视图控制器从我自己定义的'BaseViewController`扩展而来。

I am having the following question? 我有以下问题吗?

 import UIKit

 class BsgBaseViewController: UIViewController {
 var nsTimerObj: NSTimer!
 func resetTimer(addTimeToTimeOutThread: NSTimeInterval){
    nsTimerObj?.invalidate()

    let nsTimerObjTemp = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: "handleIdleEvent:", userInfo: nil, repeats: false)
    nsTimerObj = nsTimerObjTemp

 }


func handleIdleEvent(timer: NSTimer) {

        let createAccountErrorAlert: UIAlertView = UIAlertView()
        createAccountErrorAlert.delegate = self
        createAccountErrorAlert.title = ("Title")
        createAccountErrorAlert.message = "Due to the security policies your session has been timed out. We have to log you out."
        createAccountErrorAlert.addButtonWithTitle("OK")
        createAccountErrorAlert.show()

}

   override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    print("user touched")
    self.resetTimer(HardCodedValues().hardCodedTimeOutApplication)
}

override func viewDidAppear(animated: Bool) {
    self.resetTimer(HardCodedValues().hardCodedTimeOutApplication)
    print("appear")
}

override func viewWillDisappear(animated: Bool) {
    self.resetTimer(HardCodedValues().hardCodedTimeOutApplication)
    print("disappear")
}

}
  1. the method touchesBegan() does not get fired when clicking on UITextField or UITextView . 单击UITextFieldUITextView时不会触发touchesBegan()方法。

  2. I have a Settings UIViewController with a UISegmentedControl with two containers and two UIViewController . 我有一个设置UIViewControllerUISegmentedControl有两个容器和两个UIViewController All of these three UIViewController derive from my BaseViewController . 这三个UIViewController都从我的BaseViewController派生。 When I click on the other two UIViewController three instances of BaseViewController get created which I want to avoid. 当我单击其他两个UIViewController ,我想避免创建BaseViewController三个实例。

The first idea that comes to mind is to invent an InactivityMonitor class and have the app delegate create an instance of that. 我想到的第一个想法是发明一个InactivityMonitor类,并让应用程序委托创建该实例。 Let it own the timer and listen for some kind of "refresh" notification. 让它拥有计时器并侦听某种“刷新”通知。

View controllers can post those notifications when they get touches so that the timer can reset. View Controller可以在触摸时发布这些通知,以便可以重置计时器。

You probably also need your controllers to be delegates for text fields and views so that they can post notifications on that kind of activity too. 您可能还需要将控制器作为文本字段和视图的委托人,以便他们也可以针对此类活动发布通知。 In fact, the hardest part may be identifying all the user activity that needs to be noticed, depending on what kinds of UI elements you have. 实际上,最困难的部分可能是确定所有需要注意的用户活动,具体取决于您拥有哪种类型的UI元素。

The other thing I'd want to think about is how your rules should be applied on background/foreground application events. 我想考虑的另一件事是您的规则应如何应用于后台/前台应用程序事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM