Shake Detection in AppDelegate

How can I detect device jitter in AppDelegate (throughout the application) in Swift?

I found answers that describe how to do this in the view controller, but looking at how to do this in my application.

+5
source share
2 answers

Add the following snippet to AppDelegate :

 override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) { if motion == .MotionShake { print("Device shaken") } } 

Swift version 3.0:

 override func motionBegan(_ motion: UIEventSubtype, with event: UIEvent?) { if motion == .motionShake { print("Device shaken") } } 

For later versions, this no longer works. You need to add the above code to your view controller

+7
source

If you want to globally determine the movement of jitter, UIWindow implements a UIResponder that can receive a jarring event. You can add the following snippet in AppDelegate

 extension UIWindow { open override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) { if motion == .motionShake { print("Device shaken") } } } 
+6
source

Source: https://habr.com/ru/post/1240554/


All Articles