Skip to content

Commit

Permalink
Start/stop engine when entering background/foreground (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkiermasz authored Aug 12, 2024
1 parent f0d0f3e commit f2e23ad
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Sources/Pow/Infrastructure/Haptics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit f2e23ad

Please sign in to comment.