diff --git a/Sources/Amplitude/Plugins/watchOS/WatchOSLifecycleMonitor.swift b/Sources/Amplitude/Plugins/watchOS/WatchOSLifecycleMonitor.swift index f9c5d82..d87daa5 100644 --- a/Sources/Amplitude/Plugins/watchOS/WatchOSLifecycleMonitor.swift +++ b/Sources/Amplitude/Plugins/watchOS/WatchOSLifecycleMonitor.swift @@ -13,14 +13,12 @@ class WatchOSLifecycleMonitor: UtilityPlugin { var wasBackgrounded: Bool = false - private var watchExtension = WKApplication.shared() private var appNotifications: [NSNotification.Name] = [ - WKApplication.willEnterForegroundNotification, - WKApplication.didEnterBackgroundNotification, + WKExtension.applicationWillEnterForegroundNotification, + WKExtension.applicationDidEnterBackgroundNotification, ] override init() { - watchExtension = WKApplication.shared() super.init() setupListeners() } @@ -28,9 +26,9 @@ @objc func notificationResponse(notification: NSNotification) { switch notification.name { - case WKApplication.willEnterForegroundNotification: + case WKExtension.applicationWillEnterForegroundNotification: self.applicationWillEnterForeground(notification: notification) - case WKApplication.didEnterBackgroundNotification: + case WKExtension.applicationDidEnterBackgroundNotification: self.applicationDidEnterBackground(notification: notification) default: break @@ -51,18 +49,11 @@ } func applicationWillEnterForeground(notification: NSNotification) { - // watchOS will receive this after didFinishLaunching, which is different - // from iOS, so ignore until we've been backgrounded at least once. - if wasBackgrounded == false { return } - let timestamp = Int64(NSDate().timeIntervalSince1970 * 1000) self.amplitude?.onEnterForeground(timestamp: timestamp) } func applicationDidEnterBackground(notification: NSNotification) { - // make sure to denote that we were backgrounded. - wasBackgrounded = true - let timestamp = Int64(NSDate().timeIntervalSince1970 * 1000) self.amplitude?.onExitForeground(timestamp: timestamp) }