Skip to content

Commit

Permalink
πŸ› Fix crash when swizzling for push auto config
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaatttt committed Nov 26, 2024
1 parent b1dc1a4 commit 06f7327
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ extension UNUserNotificationCenter {
private func appcues__getNotificationCenterDelegate() -> UNUserNotificationCenterDelegate? {
let delegate: UNUserNotificationCenterDelegate

var shouldSetDelegate = false

// this call looks recursive, but it is not, it is calling the swapped implementation
// to get the actual delegate value that has been assigned, if any - can be nil
if let existingDelegate = appcues__getNotificationCenterDelegate() {
Expand All @@ -42,7 +44,7 @@ extension UNUserNotificationCenter {
// if it is nil, then we assign our own delegate implementation so there is
// something hooked in to listen to notifications
delegate = AppcuesUNUserNotificationCenterDelegate.shared
self.delegate = delegate
shouldSetDelegate = true
}

Swizzler.swizzle(
Expand All @@ -61,6 +63,15 @@ extension UNUserNotificationCenter {
swizzleSelector: #selector(appcues__userNotificationCenterWillPresent)
)

// If we need to set a non-nil implementation where there previously was not one,
// swap the swizzled getter back first, then assign, then restore the swizzled getter.
// This is done to avoid infinite recursion in some cases.
if shouldSetDelegate {
UNUserNotificationCenter.swizzleNotificationCenterGetDelegate()
self.delegate = delegate
UNUserNotificationCenter.swizzleNotificationCenterGetDelegate()
}

return delegate
}

Expand Down

0 comments on commit 06f7327

Please sign in to comment.