使用 NSTimer 注销应用程序
Logout application using NSTimer
我有一个登录页面,可以转到应用程序的其余部分。
在我的应用程序的其余部分中,其余的视图控制器从我自己定义的“BaseViewController”扩展而来。
我有以下问题?
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")
}
}
单击 UITextField
或 UITextView
时方法 touchesBegan()
不会触发。
我有一个设置 UIViewController
,其中 UISegmentedControl
有两个容器和两个 UIViewController
。这三个 UIViewController
都来自我的 BaseViewController
。当我单击其他两个 UIViewController
时,会创建三个 BaseViewController
实例,我想避免这样做。
想到的第一个想法是发明一个 InactivityMonitor
class 并让应用程序委托创建它的一个实例。让它拥有计时器并监听某种 "refresh" 通知。
View controllers 可以 post 在它们被触摸时收到这些通知,以便计时器可以重置。
您可能还需要将您的控制器作为文本字段和视图的委托,以便它们也可以 post 通知那种类型的 activity。事实上,最困难的部分可能是识别所有需要注意的用户 activity,具体取决于您拥有的 UI 元素类型。
我想考虑的另一件事是您的规则应如何应用于 background/foreground 应用程序事件。
我有一个登录页面,可以转到应用程序的其余部分。 在我的应用程序的其余部分中,其余的视图控制器从我自己定义的“BaseViewController”扩展而来。
我有以下问题?
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")
}
}
单击
UITextField
或UITextView
时方法touchesBegan()
不会触发。我有一个设置
UIViewController
,其中UISegmentedControl
有两个容器和两个UIViewController
。这三个UIViewController
都来自我的BaseViewController
。当我单击其他两个UIViewController
时,会创建三个BaseViewController
实例,我想避免这样做。
想到的第一个想法是发明一个 InactivityMonitor
class 并让应用程序委托创建它的一个实例。让它拥有计时器并监听某种 "refresh" 通知。
View controllers 可以 post 在它们被触摸时收到这些通知,以便计时器可以重置。
您可能还需要将您的控制器作为文本字段和视图的委托,以便它们也可以 post 通知那种类型的 activity。事实上,最困难的部分可能是识别所有需要注意的用户 activity,具体取决于您拥有的 UI 元素类型。
我想考虑的另一件事是您的规则应如何应用于 background/foreground 应用程序事件。