Daltron / NotificationBanner

The easiest way to display highly customizable in app notification banners in iOS
MIT License
4.78k stars 662 forks source link

Failed to load appWindow the app crashed #284

Closed Ahmed-Masoud closed 4 years ago

Ahmed-Masoud commented 4 years ago

this happens when i show the banner with internet connection error based on a notification from Reachability this happens when the internet is manually turned off from control center at this point the pod failes to get the correct app window and fails

darkandchoco commented 4 years ago

Same here with a new project in Xcode 11 iOS 13, it has something to do with SceneDelegate when NotificationBanner is getting the current app window. Current window is nil since NotificationBanner is not able to get a current window that is active from scene delegate.

private let appWindow: UIWindow? = { if #available(iOS 13.0, *) { return UIApplication.shared.connectedScenes .first { $0.activationState == .foregroundActive } .map { $0 as? UIWindowScene } .map { $0?.windows.first } ?? UIApplication.shared.delegate?.window ?? nil } return UIApplication.shared.delegate?.window ?? nil }()

.first { $0.activationState == .foregroundActive } results to empty array since all connected scenes are "unattached" when this is called.

darkandchoco commented 4 years ago

My workaround is to not call notification banner in app delegate but in scene delegate:

SceneDelegate:

private let reachability = try! Reachability()
private var banner: NotificationBanner?
private var isReachabilityConfigured = false

func sceneDidBecomeActive(_ scene: UIScene) {
        print("scene active")
        configureReachability()
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
    }
private func configureReachability() {
        guard !isReachabilityConfigured else { return }

        reachability.whenReachable = { [weak self] reachability in
            guard let self = self else { return }

            if let banner = self.banner {
                banner.dismiss()
            } else {
                self.banner = StatusBarNotificationBanner(title: "No Internet Connection...", style: .danger, colors: nil)
                self.banner?.autoDismiss = false
            }
        }

        reachability.whenUnreachable = { [weak self] _ in
            guard let self = self else { return }
            if let banner = self.banner {
                banner.show()
            } else {
                self.banner = StatusBarNotificationBanner(title: "No Internet Connection...", style: .danger, colors: nil)
                self.banner?.autoDismiss = false
                self.banner?.show()
            }
        }

        do {
            try reachability.startNotifier()
        } catch {
            print("Unable to start notifier")
        }

        isReachabilityConfigured = true
    }
stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 4 years ago

This issue has been automatically closed due to inactivity.

Surferdude667 commented 4 years ago

@darkandchoco Thank you so much! This totally fixed it! @Daltron