Detecting user inactivity in iOS application in Swift
how to detect the inactivity of the user in our app and perform actions? In our case, disconnecting the user.
Step: - 1
First Commend //@UIApplicationMain in AppDelegate
Step:- 2
Create one UIApplication Swift File(NSObject File)
Coding in UIApplication File
Playground - noun: a place where people can play
import UIKit
extension NSNotification.Name {
public static let TimeOutUserInteraction: NSNotification.Name = NSNotification.Name(rawValue: "MTopoUserUIApplication")
}
class MTopoUserUIApplication: UIApplication {
static let ApplicationDidTimoutNotification = "AppTimout"
// The timeout in seconds for when to fire the idle timer.
let timeoutInSeconds: TimeInterval = 10
var idleTimer: Timer?
// Listen for any touch. If the screen receives a touch, the timer is reset.
override func sendEvent(_ event: UIEvent) {
super.sendEvent(event)
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 {
idleTimer.invalidate()
}
idleTimer = Timer.scheduledTimer(timeInterval: timeoutInSeconds, target: self, selector: #selector(self.idleTimerExceeded), userInfo: nil, repeats: false)
}
// If the timer reaches the limit as defined in timeoutInSeconds, post this notification.
func idleTimerExceeded() {
NotificationCenter.default.post(name:Notification.Name.TimeOutUserInteraction, object: nil)
}
}
Step:- 3
Create one Main.Swift File(NSObject Class)
//Coding in Swift
CommandLine.unsafeArgv.withMemoryRebound(to: UnsafeMutablePointer<Int8>.self, capacity: Int(CommandLine.argc)) {argv in
_ = UIApplicationMain(CommandLine.argc, argv, NSStringFromClass(MTopoUserUIApplication.self), NSStringFromClass(AppDelegate.self))
}
Step :- 4
Using This Code Any ViewController in File .It can Be Working Any View Controller File
//Coding in View Controller
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(userIdle), name: Notification.Name.TimeOutUserInteraction, object: nil)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func secVC(_ sender: Any) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "SecVC") as! SecVC
self.navigationController?.pushViewController(newViewController, animated: true)
}
func userIdle() {
print("Delayed")
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.navigationController?.pushViewController(newViewController, animated: true)
}
}
Comments
Post a Comment