ArtSabintsev / Siren

Notify users when a new version of your app is available and prompt them to upgrade.
http://sabintsev.com/
MIT License
4.26k stars 407 forks source link

Don't appear dialog when ckeck update as .onForeground #396

Closed keyboardseokmin closed 2 years ago

keyboardseokmin commented 2 years ago

Info

Problem

  1. Check update as .onForeground (Siren.shared.wail(performCheck: .onForeground))
  2. If user swipe down the notification center screen to the bottom of screen (by swiping downward from the very top of the device's screen)
  3. And back to app can't appear update dialog

Reason

If swipe down the notification center screen to the bottom of screen iOS call observer like this

  1. starting to swipe down : willResignActiveNotification
  2. reached to bottom : didBecomeActiveNotification, willResignActiveNotification

What i solved

private var appDidBecomeActiveWorkItem: DispatchWorkItem?

private extension Siren {
    /// Adds an observer that listens for app launching/relaunching.
    func addForegroundObservers() {
        guard applicationDidBecomeActiveObserver == nil else { return }
        applicationDidBecomeActiveObserver = NotificationCenter
            .default
            .addObserver(forName: UIApplication.didBecomeActiveNotification,
                         object: nil,
                         queue: nil) { [weak self] _ in
                guard let self = self else { return }
                self.appDidBecomeActiveWorkItem = DispatchWorkItem {
                    self.performVersionCheck()
                }
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.02, execute: self.appDidBecomeActiveWorkItem!)
        }
    }

    /// Adds an observer that listens for when the user enters the app switcher
    /// and when the app is sent to the background.
    func addBackgroundObservers() {
        if applicationWillResignActiveObserver == nil {
            applicationWillResignActiveObserver = NotificationCenter
                .default
                .addObserver(forName: UIApplication.willResignActiveNotification,
                             object: nil,
                             queue: nil) { [weak self] _ in
                    guard let self = self else { return }
                    self.appDidBecomeActiveWorkItem?.cancel()
                    self.appDidBecomeActiveWorkItem = nil

                    self.presentationManager.cleanUp()
            }
        }

        if applicationDidEnterBackgroundObserver == nil {
            applicationDidEnterBackgroundObserver = NotificationCenter
                .default
                .addObserver(forName: UIApplication.didEnterBackgroundNotification,
                             object: nil,
                             queue: nil) { [weak self] _ in
                                guard let self = self else { return }
                                self.presentationManager.cleanUp()
            }
        }
    }
}
ArtSabintsev commented 2 years ago

Can you open a PR to show the differences? Thanks.

keyboardseokmin commented 2 years ago

Can you open a PR to show the differences? Thanks.

Of course, just opened a PR : )