Last active
December 6, 2018 13:27
-
-
Save designablebits/b0ff269ebdddefc4599166839a102e48 to your computer and use it in GitHub Desktop.
Check user inactivity/idle time since last screen touch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppDelegate.swift | |
// Let's do this app | |
// | |
// Created by Sanjeev on 06.12.18. | |
// Copyright © 2018 Sanjeev. All rights reserved. | |
// | |
import UIKit | |
//@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { | |
NotificationCenter.default.addObserver(self, | |
selector: #selector(getBackToRootScreen), | |
name: .TimeOutUserInteraction, | |
object: nil) | |
return true | |
} | |
func applicationDidEnterBackground(_ application: UIApplication) { | |
} | |
func applicationWillResignActive(_ application: UIApplication) { | |
let date = NSDate() | |
let resignTime = Int64(date.timeIntervalSince1970) | |
print("\(#function) was called and resign time is \(resignTime)") | |
UserDefaults.standard.set(resignTime, forKey: "logOutTime") | |
} | |
func applicationDidBecomeActive(_ application: UIApplication) { | |
if let resignTime = UserDefaults.standard.object(forKey: "logOutTime") as? Int64 { | |
let date = NSDate() | |
let currentTime = Int64(date.timeIntervalSince1970) | |
debugPrint("diff: ", currentTime-resignTime) | |
print("\(#function) was called and current time is \(currentTime)") | |
let diff = currentTime - (resignTime + Int64(ticker)) | |
if diff >= (15 * 60) { //checking difference of time which you expect | |
getBackToRootScreen() | |
UserDefaults.standard.set(0, forKey: "logOutTime") | |
} | |
} | |
} | |
func applicationWillTerminate(_ application: UIApplication) { | |
let date = NSDate() | |
let resignTime = Int64(date.timeIntervalSince1970) | |
print("\(#function) was called and resign time is \(resignTime)") | |
UserDefaults.standard.set(resignTime, forKey: "logOutTime") | |
} | |
@objc func getBackToRootScreen(){ | |
print("Observed time out") | |
self.window = UIWindow(frame: UIScreen.main.bounds) | |
let loginScreen = LoginViewController(nibName: "LoginViewController", bundle: nil) | |
let navigation = UINavigationController(rootViewController: loginScreen) | |
self.window?.rootViewController = navigation | |
self.window?.makeKeyAndVisible() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// InterractionUIApplication.swift | |
// Let's do this app | |
// | |
// Created by Sanjeev on 06.12.18. | |
// Copyright © 2018 Sanjeev. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
var ticker = 0 | |
extension NSNotification.Name { | |
public static let TimeOutUserInteraction: NSNotification.Name = NSNotification.Name(rawValue: "TimeOutUserInteraction") | |
} | |
class InterractionUIApplication: UIApplication { | |
static let ApplicationDidTimoutNotification = "AppTimout" | |
// The timeout in seconds for when to fire the idle timer. | |
let timeoutInSeconds: TimeInterval = 15 * 60 | |
var idleTimer: Timer? | |
var timer = Timer() | |
// Listen for any touch. If the screen receives a touch, the timer is reset. | |
override func sendEvent(_ event: UIEvent) { | |
super.sendEvent(event) | |
// print("3")/ | |
if idleTimer != nil { | |
self.resetIdleTimer() | |
} | |
if let touches = event.allTouches { | |
for touch in touches { | |
if touch.phase == UITouchPhase.began { | |
self.resetIdleTimer() | |
} | |
} | |
} | |
} | |
// Resent the timer because there was user interaction. | |
func resetIdleTimer() { | |
if let idleTimer = idleTimer { | |
// print("1") | |
// let date = NSDate() | |
// let resignTime = Int64(date.timeIntervalSince1970) | |
// print("\(#function) was called and resign time is \(resignTime)") | |
// UserDefaults.standard.set(resignTime, forKey: "logOutTime") | |
idleTimer.invalidate() | |
timer.invalidate() | |
ticker = 0 | |
} | |
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(printTime), userInfo: nil, repeats: true) | |
idleTimer = Timer.scheduledTimer(timeInterval: timeoutInSeconds, target: self, selector: #selector(self.idleTimerExceeded), userInfo: nil, repeats: false) | |
} | |
@objc func printTime(){ | |
ticker = ticker + 1 | |
print(ticker) | |
} | |
// If the timer reaches the limit as defined in timeoutInSeconds, post this notification. | |
@objc func idleTimerExceeded() { | |
let timeOutStr = "Time Out" | |
print(timeOutStr) | |
NotificationCenter.default.post(name:Notification.Name.TimeOutUserInteraction, object: nil) | |
//Go Main page after 15 minutes | |
let appDelegate = UIApplication.shared.delegate as! AppDelegate | |
appDelegate.window = UIWindow(frame: UIScreen.main.bounds) | |
let loginScreen = LoginViewController(nibName: "LoginViewController", bundle: nil) | |
let navigation = UINavigationController(rootViewController: loginScreen) | |
appDelegate.window?.rootViewController = navigation | |
appDelegate.window?.makeKeyAndVisible(); | |
UIManager.shareUIManager.rootController() | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// Let's do this app | |
// | |
// Created by Sanjeev on 06.12.18. | |
// Copyright © 2018 Sanjeev. All rights reserved. | |
// | |
import UIKit | |
CommandLine.unsafeArgv.withMemoryRebound(to: UnsafeMutablePointer<Int8>.self, capacity: Int(CommandLine.argc)) | |
{ argv in | |
_ = UIApplicationMain(CommandLine.argc, argv, NSStringFromClass(InterractionUIApplication.self), NSStringFromClass(AppDelegate.self)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment