ArtSabintsev / Siren

Notify users when a new version of your app is available and prompt them to upgrade.

Home Page:http://sabintsev.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Don't appear dialog when ckeck update as .onForeground

keyboardseokmin opened this issue · comments

Info

  • Siren Version: 5.8.1
  • Test device: iphone7, version 15.0

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()
            }
        }
    }
}

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

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

Of course, just opened a PR : )