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

Commit 495a1e2

Browse files
committed
make widget transparent for iPad
1 parent ae3e2c9 commit 495a1e2

File tree

5 files changed

+65
-34
lines changed

5 files changed

+65
-34
lines changed

Example/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- WeLoop (0.2.2)
2+
- WeLoop (0.2.3)
33

44
DEPENDENCIES:
55
- WeLoop (from `../`)
@@ -9,8 +9,8 @@ EXTERNAL SOURCES:
99
:path: "../"
1010

1111
SPEC CHECKSUMS:
12-
WeLoop: 4b43e35ee4ef8778becd782fccb58bae7b9d5351
12+
WeLoop: 4957dcb973aea1b54a3c5796904bc1c9a27ceed3
1313

1414
PODFILE CHECKSUM: f2bbf31f07ce3fbb7efbfdc77822603ebfe51121
1515

16-
COCOAPODS: 1.6.1
16+
COCOAPODS: 1.7.1

Example/WeLoop.xcodeproj/project.pbxproj

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,11 @@
258258
buildActionMask = 2147483647;
259259
files = (
260260
);
261-
inputFileListPaths = (
262-
);
263261
inputPaths = (
264262
"${PODS_ROOT}/Target Support Files/Pods-WeLoop_Example/Pods-WeLoop_Example-frameworks.sh",
265263
"${BUILT_PRODUCTS_DIR}/WeLoop/WeLoop.framework",
266264
);
267265
name = "[CP] Embed Pods Frameworks";
268-
outputFileListPaths = (
269-
);
270266
outputPaths = (
271267
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/WeLoop.framework",
272268
);
@@ -490,6 +486,7 @@
490486
PRODUCT_NAME = "$(TARGET_NAME)";
491487
PROVISIONING_PROFILE_SPECIFIER = "";
492488
SWIFT_VERSION = 5.0;
489+
TARGETED_DEVICE_FAMILY = "1,2";
493490
};
494491
name = Debug;
495492
};
@@ -508,6 +505,7 @@
508505
PRODUCT_NAME = "$(TARGET_NAME)";
509506
PROVISIONING_PROFILE_SPECIFIER = "";
510507
SWIFT_VERSION = 5.0;
508+
TARGETED_DEVICE_FAMILY = "1,2";
511509
};
512510
name = Release;
513511
};

Example/WeLoop/Info.plist

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
<string>1</string>
2323
<key>LSRequiresIPhoneOS</key>
2424
<true/>
25+
<key>NSCameraUsageDescription</key>
26+
<string>WeLoop needs to access your camera to take pictures</string>
27+
<key>NSPhotoLibraryUsageDescription</key>
28+
<string>WeLoop needs to access your library to share photos</string>
2529
<key>UILaunchStoryboardName</key>
2630
<string>LaunchScreen</string>
2731
<key>UIMainStoryboardFile</key>
@@ -34,12 +38,10 @@
3438
<array>
3539
<string>UIInterfaceOrientationPortrait</string>
3640
<string>UIInterfaceOrientationLandscapeLeft</string>
41+
<string>UIInterfaceOrientationLandscapeRight</string>
42+
<string>UIInterfaceOrientationPortraitUpsideDown</string>
3743
</array>
3844
<key>UIViewControllerBasedStatusBarAppearance</key>
3945
<true/>
40-
<key>NSPhotoLibraryUsageDescription</key>
41-
<string>WeLoop needs to access your library to share photos</string>
42-
<key>NSCameraUsageDescription</key>
43-
<string>WeLoop needs to access your camera to take pictures</string>
4446
</dict>
4547
</plist>

WeLoop/Classes/WeLoop.swift

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ public class WeLoop: NSObject {
5656

5757
// MARK: Object references
5858

59-
/// A reference to the previous controller in the window when the widget is invoked. Used to restore
59+
/// A reference to the previous window when the widget is invoked. Used to restore
6060
/// the app's state when the widget is dismissed
61-
private var previousViewController: UIViewController?
61+
private var previousWindow: UIWindow?
62+
63+
/// A reference to the widget with containing the webview
64+
private var weLoopViewController: WeLoopViewController?
6265

6366
/// A reference to the controller containing the floating action button
6467
private var fabController: FloatingButtonController?
@@ -131,6 +134,11 @@ public class WeLoop: NSObject {
131134
super.init()
132135
}
133136

137+
var isShowingWidget: Bool {
138+
guard let widgetVC = weLoopViewController else { return false }
139+
return widgetVC.window.isKeyWindow && previousWindow == nil
140+
}
141+
134142
func initialize(apiKey: String, autoAuthentication: Bool = true, subdomain: String? = nil) {
135143
self.apiKey = apiKey
136144
self.autoAuthentication = autoAuthentication
@@ -143,6 +151,7 @@ public class WeLoop: NSObject {
143151
let project = try project()
144152
self.project = project
145153
self.setupInvocation(settings: project.settings)
154+
try self.initializeWidget()
146155
self.delegate?.initializationSuccessful?()
147156
} catch (let error) {
148157
self.authenticationError = error
@@ -166,28 +175,14 @@ public class WeLoop: NSObject {
166175

167176
@objc func invokeSelector() {
168177
do {
169-
// Another instance of the widget is already present.
170-
guard previousViewController == nil else { return }
171-
172-
let url = try widgetURL()
173-
let widgetVC = WeLoopViewController()
174-
widgetVC.url = url
175-
try showWidget(viewController: widgetVC)
176-
178+
guard !isShowingWidget else { return }
179+
try showWidget()
177180
} catch (let error) {
178181
print(error)
179182
delegate?.failedToLaunch?(with: error)
180183
}
181184
}
182185

183-
/// Close the widget, and show the previous view controller instead
184-
func close() {
185-
guard let viewController = previousViewController, let project = project, let window = UIApplication.shared.keyWindow else { return }
186-
previousViewController = nil
187-
window.rootViewController = viewController
188-
setupInvocation(settings: project.settings)
189-
}
190-
191186
private func setupInvocation(settings: Settings) {
192187
switch invocationMethod {
193188
case .shakeGesture:
@@ -212,13 +207,31 @@ public class WeLoop: NSObject {
212207
}
213208
}
214209

215-
private func showWidget(viewController: WeLoopViewController) throws {
210+
private func initializeWidget() throws {
211+
let url = try widgetURL()
212+
let widgetVC = WeLoopViewController()
213+
widgetVC.url = url
214+
self.weLoopViewController = widgetVC
215+
}
216+
217+
private func showWidget() throws {
216218
guard let keyWindow = UIApplication.shared.keyWindow else { throw WeLoopError.windowMissing }
217219

218220
screenshot = keyWindow.takeScreenshot()
219221
disableInvocation(method: invocationMethod)
220-
previousViewController = keyWindow.rootViewController
221-
UIApplication.shared.keyWindow?.rootViewController = viewController
222+
keyWindow.resignKey()
223+
weLoopViewController?.window.becomeKey()
224+
weLoopViewController?.window.isHidden = false
225+
previousWindow = keyWindow
226+
}
227+
228+
/// Close the widget, and show the previous window instead
229+
func closeWidget() {
230+
guard let project = project, let window = previousWindow else { return }
231+
weLoopViewController?.window.resignKey()
232+
weLoopViewController?.window.isHidden = true
233+
window.makeKeyAndVisible()
234+
setupInvocation(settings: project.settings)
222235
}
223236

224237
private func widgetURL() throws -> URL {

WeLoop/Classes/WeLoopViewController.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class WeLoopViewController: UIViewController {
1919

2020
var url: URL?
2121
weak var webView: WKWebView!
22-
22+
var window: UIWindow = UIWindow(frame: UIScreen.main.bounds)
23+
2324
lazy var configuration: WKWebViewConfiguration = {
2425
let config = WKWebViewConfiguration()
2526
let userController = WKUserContentController()
@@ -32,14 +33,31 @@ class WeLoopViewController: UIViewController {
3233
return true
3334
}
3435

36+
init() {
37+
super.init(nibName: nil, bundle: nil)
38+
window.windowLevel = UIWindow.Level(rawValue: CGFloat.greatestFiniteMagnitude)
39+
window.isHidden = true
40+
window.rootViewController = self
41+
}
42+
43+
required init?(coder aDecoder: NSCoder) {
44+
fatalError("init(coder:) has not been implemented")
45+
}
46+
3547
override func viewDidLoad() {
3648
super.viewDidLoad()
49+
view.backgroundColor = .clear
3750
configureWebview()
3851
}
3952

4053
private func configureWebview() {
4154
let webView = FullScreenWKWebView(frame: view.bounds, configuration: self.configuration)
4255
view.addSubview(webView)
56+
57+
webView.isOpaque = false
58+
webView.backgroundColor = .clear
59+
webView.scrollView.backgroundColor = .clear
60+
4361
webView.allowsLinkPreview = false
4462
webView.scrollView.delegate = self
4563
webView.scrollView.bounces = false
@@ -61,7 +79,7 @@ class WeLoopViewController: UIViewController {
6179
}
6280

6381
@objc func back() {
64-
WeLoop.shared.close()
82+
WeLoop.shared.closeWidget()
6583
}
6684

6785
func sendScreenshot() {

0 commit comments

Comments
 (0)