Skip to content

Commit 4537cbd

Browse files
committed
Remove UIApplication.shared to make agent usable for extensions
1 parent fa4bf5d commit 4537cbd

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

Sources/InstanaAgent/Utils/InstanaApplicationStateHandler.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,41 @@ import UIKit
33

44
// We need an abstract state handler to make the Notifications (didBecomeActiveNotification, ...) easier to test
55
class InstanaApplicationStateHandler {
6+
7+
enum State {
8+
case active
9+
case inactive
10+
case background
11+
}
12+
613
static let shared = InstanaApplicationStateHandler()
714

8-
@Atomic var state: UIApplication.State = UIApplication.shared.applicationState {
15+
@Atomic var state: State = .inactive {
916
didSet { stateUpdateHandler.forEach { $0(state) } }
1017
}
1118

12-
typealias StateUpdater = (UIApplication.State) -> Void
19+
typealias StateUpdater = (State) -> Void
1320
private var stateUpdateHandler = [StateUpdater]()
1421

1522
init() {
1623
_ = NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: nil) { _ in
1724
self.state = .active
1825
}
26+
_ = NotificationCenter.default.addObserver(forName: NSNotification.Name.NSExtensionHostDidBecomeActive, object: nil, queue: nil) { _ in
27+
self.state = .active
28+
}
1929
_ = NotificationCenter.default.addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: nil) { _ in
2030
self.state = .background
2131
}
22-
32+
_ = NotificationCenter.default.addObserver(forName: NSNotification.Name.NSExtensionHostDidEnterBackground, object: nil, queue: nil) { _ in
33+
self.state = .background
34+
}
2335
_ = NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: nil) { _ in
2436
self.state = .inactive
2537
}
38+
_ = NotificationCenter.default.addObserver(forName: NSNotification.Name.NSExtensionHostWillResignActive, object: nil, queue: nil) { _ in
39+
self.state = .inactive
40+
}
2641
}
2742

2843
func listen(_ handler: @escaping StateUpdater) {

0 commit comments

Comments
 (0)