Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct behaviors around single-target watch apps vs watch extensions to avoid crashes when targeting watchOS 8 and lower #179

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions Sources/Amplitude/Plugins/watchOS/WatchOSLifecycleMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,57 @@
class WatchOSLifecycleMonitor: UtilityPlugin {
var wasBackgrounded: Bool = false

private var watchExtension = WKApplication.shared()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't referenced anywhere

private var appNotifications: [NSNotification.Name] = [
WKApplication.willEnterForegroundNotification,
WKApplication.didEnterBackgroundNotification,
]
var isSingleTargetApplication: Bool {
return Bundle.main.infoDictionary?.keys.contains("WKApplication") == true
}

private var appNotifications: [NSNotification.Name] {
if #available(watchOS 9.0, *) {
// `WKApplication` works on both dual-target and single-target apps
// When running on watchOS 9.0+
return [
WKApplication.willEnterForegroundNotification,
WKApplication.didEnterBackgroundNotification
]
} else if !isSingleTargetApplication {
return [
WKExtension.applicationWillEnterForegroundNotification,
WKExtension.applicationDidEnterBackgroundNotification
]
} else {
// Before watchOS 9.0, single-target apps don't allow using `WKExtension` or `WKApplication`
// So we can't utilize any notifications
return []
}
}

override init() {
watchExtension = WKApplication.shared()
super.init()
setupListeners()
}

@objc
func notificationResponse(notification: NSNotification) {
switch notification.name {
case WKApplication.willEnterForegroundNotification:
self.applicationWillEnterForeground(notification: notification)
case WKApplication.didEnterBackgroundNotification:
self.applicationDidEnterBackground(notification: notification)
default:
break
if #available(watchOS 9.0, *) {
switch notification.name {
case WKApplication.willEnterForegroundNotification:
self.applicationWillEnterForeground(notification: notification)
case WKApplication.didEnterBackgroundNotification:
self.applicationDidEnterBackground(notification: notification)
default:
break
}
} else if !isSingleTargetApplication {
switch notification.name {
case WKExtension.applicationWillEnterForegroundNotification:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splitting this out is probably overkill but wanted to be extra safe around when WKApplication or WKExtension was referenced.

self.applicationWillEnterForeground(notification: notification)
case WKExtension.applicationDidEnterBackgroundNotification:
self.applicationDidEnterBackground(notification: notification)
default:
break
}
} else {
return
}
}

Expand Down
Loading