Skip to content

Commit

Permalink
šŸ› Add fallback in the case where thereā€™s no active window scene
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaatttt committed Nov 8, 2024
1 parent ae53fa3 commit f627f69
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class AppcuesRequestReviewAction: AppcuesExperienceAction {
}

func execute(completion: @escaping ActionRegistry.Completion) {
if #available(iOS 14.0, *), let windowScene = UIApplication.shared.activeWindowScenes.first {
if #available(iOS 14.0, *), let windowScene = UIApplication.shared.mainWindowScene {
SKStoreReviewController.requestReview(in: windowScene)
} else {
SKStoreReviewController.requestReview()
Expand Down
4 changes: 2 additions & 2 deletions Sources/AppcuesKit/Presentation/Debugger/UIDebugger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ internal class UIDebugger: UIDebugging {

// Set up the debugger

guard let windowScene = UIApplication.shared.activeWindowScenes.first else {
guard let windowScene = UIApplication.shared.mainWindowScene else {
config.logger.error("Could not open debugger")
return
}
Expand Down Expand Up @@ -161,7 +161,7 @@ internal class UIDebugger: UIDebugging {
}

// One-time on-demand set up of the toast window
if toastWindow == nil, let windowScene = UIApplication.shared.activeWindowScenes.first {
if toastWindow == nil, let windowScene = UIApplication.shared.mainWindowScene {
toastWindow = ToastUIWindow(windowScene: windowScene)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ internal class ModalContextManager {
return
}

guard let windowScene = UIApplication.shared.activeWindowScenes.first else {
throw AppcuesTraitError(description: "No active window scene")
guard let windowScene = UIApplication.shared.mainWindowScene else {
throw AppcuesTraitError(description: "No main window scene")
}

let window = AppcuesUIWindow(windowScene: windowScene)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ internal protocol URLOpening {
extension UIApplication: TopControllerGetting {

@available(iOS 13.0, *)
var activeWindowScenes: [UIWindowScene] {
private var activeWindowScenes: [UIWindowScene] {
self.connectedScenes
.filter { $0.activationState == .foregroundActive }
.compactMap { $0 as? UIWindowScene }
}

// Prefer the active window scene, but in the case where there's no active scene, use the one from the main app window.
@available(iOS 13.0, *)
var mainWindowScene: UIWindowScene? {
activeWindowScenes.first ?? windows.first(where: { !$0.isAppcuesWindow })?.windowScene
}

// We expose this property because a unit test cannot init a UIWindowScene for mocking different states.
var hasActiveWindowScenes: Bool {
if #available(iOS 13.0, *) {
Expand Down

0 comments on commit f627f69

Please sign in to comment.