iOS:全局检测键盘输入

  • 本文关键字:键盘 全局 iOS ios swift
  • 更新时间 :
  • 英文 :


如果我的应用程序在一定时间内未使用,我想注销我的用户。对于触摸交互,我可以在UIWindow上覆盖sendEvent。但是对于键盘输入,它不起作用。

是否可以在用户使用键盘时接收事件?(我不想子类UITextField等,因为我的应用程序中也有WebView(。

您可以覆盖UIWindowhitTest- 它还处理系统键盘触摸:

extension UIWindow {
override open func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
print("touched!")
return super.hitTest(point, with: event)
}
}

也许断开用户连接的更好方法是使用func applicationWillResignActive(_ application: UIApplication)func applicationDidEnterBackground(_ application: UIApplication)

为了防止用户与键盘交互,您可以实现UITextFieldDelegate,通过实例使用以下函数,可以让您知道用户何时键入字符,最后一次:func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool

最新更新