@@ -44,7 +44,10 @@ public class WeLoop: NSObject {
4444
4545 /// The preferred invocation method for the SDK. Must be set using `setInvocationMethod`
4646 var invocationMethod : WeLoopInvocation = . manual
47-
47+
48+ /// If set to true, weloop windows are attached to your top most foreground active scene
49+ var sceneBasedApplication : Bool = false
50+
4851 // MARK: Object references
4952
5053 /// A reference to the previous window when the widget is invoked. Used to restore
@@ -57,6 +60,9 @@ public class WeLoop: NSObject {
5760 /// A reference to the controller containing the floating action button
5861 private var fabController : FloatingButtonController ?
5962
63+ /// A reference to the window used to present the Weloop interface
64+ private var popupWindow : UIWindow ?
65+
6066 /// A screenshot of the window is taken right before invoking the SDK. This is a ref to this screenshot
6167 var screenshot : UIImage ?
6268
@@ -66,6 +72,7 @@ public class WeLoop: NSObject {
6672 static let shared = WeLoop ( )
6773
6874
75+
6976 // MARK: - Public API
7077
7178
@@ -93,6 +100,10 @@ public class WeLoop: NSObject {
93100 shared. set ( invocationMethod: method)
94101 }
95102
103+ @objc public static func set( sceneBasedApplication: Bool ) {
104+ shared. sceneBasedApplication = sceneBasedApplication
105+ }
106+
96107 /// Manually invoke the WeLoop widget.
97108 @objc public static func invoke( ) {
98109 shared. invokeSelector ( )
@@ -182,8 +193,7 @@ public class WeLoop: NSObject {
182193 // MARK: Widget
183194
184195 var isShowingWidget : Bool {
185- guard let widgetVC = weLoopViewController else { return false }
186- return widgetVC. window. isKeyWindow && previousWindow == nil
196+ return popupWindow? . isKeyWindow ?? false && previousWindow == nil
187197 }
188198
189199 private func initializeWidget( ) throws {
@@ -193,24 +203,40 @@ public class WeLoop: NSObject {
193203 // Forces the pre-load of the webview
194204 widgetVC. loadViewIfNeeded ( )
195205 self . weLoopViewController = widgetVC
206+ try configurePopupWindow ( )
207+ }
208+
209+ private func configurePopupWindow( ) throws {
210+ if #available( iOS 13 . 0 , * ) , self . sceneBasedApplication {
211+ let scene = UIApplication . shared. connectedScenes. filter { $0. activationState == . foregroundActive } . first
212+ guard let windowScene = scene as? UIWindowScene else { throw WeLoopError . windowMissing }
213+ popupWindow = UIWindow ( frame: windowScene. coordinateSpace. bounds)
214+ popupWindow? . windowScene = windowScene
215+ } else {
216+ popupWindow = UIWindow ( frame: UIScreen . main. bounds)
217+ }
218+
219+ popupWindow? . backgroundColor = . clear
220+ popupWindow? . windowLevel = . statusBar + 1
221+ popupWindow? . rootViewController = weLoopViewController
196222 }
197223
198224 private func showWidget( ) throws {
199225 guard let keyWindow = UIApplication . shared. keyWindow else { throw WeLoopError . windowMissing }
200226
201227 screenshot = keyWindow. takeScreenshot ( )
202228 keyWindow. resignKey ( )
203- weLoopViewController ? . window . becomeKey ( )
204- weLoopViewController ? . window . isHidden = false
229+ popupWindow ? . becomeKey ( )
230+ popupWindow ? . isHidden = false
205231 previousWindow = keyWindow
206232 disableInvocation ( method: invocationMethod)
207233 }
208234
209235 /// Close the widget, and show the previous window instead
210236 func closeWidget( ) {
211237 guard let settings = settings, let window = previousWindow else { return }
212- weLoopViewController ? . window . resignKey ( )
213- weLoopViewController ? . window . isHidden = true
238+ popupWindow ? . resignKey ( )
239+ popupWindow ? . isHidden = true
214240 window. makeKeyAndVisible ( )
215241 setupInvocation ( settings: settings)
216242 }
0 commit comments