Start/stop engine when entering background/foreground (#70)

This commit is contained in:
Jakub Kiermasz 2024-08-12 20:45:25 +02:00 committed by GitHub
parent f0d0f3e72d
commit f2e23ad418
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5,8 +5,21 @@ import CoreHaptics
internal struct Haptics {
private static var engine: CHHapticEngine? = {
return try? CHHapticEngine()
let engine = try? CHHapticEngine()
addHapticEngineObservers()
return engine
}()
private static func addHapticEngineObservers() {
// Without stopping the CHHapticEngine when entering background mode, haptics are not played when the app enters the foreground.
// See https://github.com/EmergeTools/Pow/issues/69
NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: nil) { _ in
engine?.stop()
}
NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: nil) { _ in
try? engine?.start()
}
}
private static var supportsHaptics = CHHapticEngine.capabilitiesForHardware().supportsHaptics