Skip to content
This repository was archived by the owner on Jun 4, 2025. It is now read-only.

Commit 00cee16

Browse files
committed
fix compatiblity with scene delegates
1 parent eaf2dc7 commit 00cee16

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

WeLoop/Classes/FloatingButtonController.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,21 @@ private class FloatingButtonWindow: UIWindow {
7676
var button: UIButton?
7777

7878
init() {
79-
super.init(frame: UIScreen.main.bounds)
79+
if #available(iOS 13.0, *), WeLoop.shared.sceneBasedApplication {
80+
let scene = UIApplication.shared
81+
.connectedScenes
82+
.filter { $0.activationState == .foregroundActive }
83+
.first
84+
85+
guard let windowScene = scene as? UIWindowScene else {
86+
super.init(frame: UIScreen.main.bounds)
87+
return
88+
}
89+
super.init(frame: windowScene.coordinateSpace.bounds)
90+
self.windowScene = windowScene
91+
} else {
92+
super.init(frame: UIScreen.main.bounds)
93+
}
8094
backgroundColor = nil
8195
}
8296

WeLoop/Classes/WeLoop.swift

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

WeLoop/Classes/WeLoopViewController.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class WeLoopViewController: UIViewController {
2222
var url: URL?
2323
weak var webView: WKWebView!
2424
weak var indicator: UIActivityIndicatorView!
25-
var window: UIWindow = UIWindow(frame: UIScreen.main.bounds)
2625
private var observation: NSKeyValueObservation?
2726

2827
lazy var configuration: WKWebViewConfiguration = {
@@ -39,9 +38,6 @@ class WeLoopViewController: UIViewController {
3938

4039
init() {
4140
super.init(nibName: nil, bundle: nil)
42-
window.windowLevel = UIWindow.Level(rawValue: CGFloat.greatestFiniteMagnitude)
43-
window.isHidden = true
44-
window.rootViewController = self
4541
}
4642

4743
deinit {

0 commit comments

Comments
 (0)