-
Notifications
You must be signed in to change notification settings - Fork 3
Add Lockdown Mode warning sheet for iOS editor #418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
ios/Sources/GutenbergKit/Sources/Services/LockdownModeMonitor.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| import Foundation | ||
| import SwiftUI | ||
| import WebKit | ||
| import OSLog | ||
|
|
||
| #if canImport(UIKit) | ||
| import UIKit | ||
|
|
||
| /// Protocol for objects that can be checked for Lockdown Mode status. | ||
| /// | ||
| /// This protocol enables testability by allowing mock implementations | ||
| /// that simulate different Lockdown Mode states. | ||
| @MainActor | ||
| protocol LockdownModeDetectable: AnyObject { | ||
| /// Returns `true` if Lockdown Mode is enabled for this object. | ||
| var isLockdownModeEnabled: Bool { get } | ||
| } | ||
|
|
||
| /// Extension to make WKWebView conform to LockdownModeDetectable. | ||
| extension WKWebView: LockdownModeDetectable { | ||
| var isLockdownModeEnabled: Bool { | ||
| configuration.defaultWebpagePreferences.isLockdownModeEnabled | ||
| } | ||
| } | ||
|
|
||
| /// Monitors Lockdown Mode status and presents warning UI when needed. | ||
| /// | ||
| /// This class handles detection of iOS Lockdown Mode in the WebView and manages | ||
| /// the presentation of a warning sheet to inform users about potential editor limitations. | ||
| @MainActor | ||
| class LockdownModeMonitor: ObservableObject { | ||
|
|
||
| @Published | ||
| public var isLockdownModeEnabled: Bool | ||
|
|
||
| /// Indicates whether the Lockdown Mode sheet has been shown to the user. | ||
| private var hasShownSheet = false | ||
|
|
||
| /// Indicates whether we should show the lockdown sheet on next editor load. | ||
| private var shouldShowSheet = false | ||
|
|
||
| /// Weak reference to the view controller that will present the sheet. | ||
| private weak var presentingViewController: UIViewController? | ||
|
|
||
| /// Weak reference to the detectable object for re-checking on foreground. | ||
| private weak var detectable: LockdownModeDetectable? | ||
|
|
||
| init(isLockdownModeEnabled: Bool = false) { | ||
| self.isLockdownModeEnabled = isLockdownModeEnabled | ||
| } | ||
|
|
||
| deinit { | ||
| NotificationCenter.default.removeObserver(self) | ||
| } | ||
|
|
||
| /// Detects Lockdown Mode status in the detectable object and triggers sheet presentation if needed. | ||
| /// | ||
| /// - Parameter detectable: The object to check for Lockdown Mode status. | ||
| public func detectLockdownMode(for detectable: LockdownModeDetectable) { | ||
| Logger.navigation.debug("Detecting Lockdown Mode") | ||
|
|
||
| // Store weak reference to detectable object for later use (foreground reloads) | ||
| self.detectable = detectable | ||
|
|
||
| let wasEnabled = self.isLockdownModeEnabled | ||
| self.isLockdownModeEnabled = detectable.isLockdownModeEnabled | ||
|
|
||
| // Handle transition from disabled to enabled: show sheet | ||
| if self.isLockdownModeEnabled && !wasEnabled && !hasShownSheet { | ||
| shouldShowSheet = true | ||
| } | ||
|
|
||
| // Handle transition from enabled to disabled: clear sheet state | ||
| // This happens when user excludes app from Lockdown Mode | ||
| if !self.isLockdownModeEnabled && wasEnabled { | ||
| hasShownSheet = false | ||
| shouldShowSheet = false | ||
| } | ||
| } | ||
|
|
||
| /// Sets up the monitor with required dependencies and starts observing foreground notifications. | ||
| /// | ||
| /// - Parameter viewController: The view controller to use for sheet presentation. | ||
| public func setup(presentingViewController viewController: UIViewController) { | ||
| self.presentingViewController = viewController | ||
|
|
||
| // Observe foreground notifications to re-check Lockdown Mode | ||
| NotificationCenter.default.addObserver( | ||
| self, | ||
| selector: #selector(handleWillEnterForeground), | ||
| name: UIApplication.willEnterForegroundNotification, | ||
| object: nil | ||
| ) | ||
| } | ||
|
|
||
| @objc private func handleWillEnterForeground() { | ||
| guard let detectable else { return } | ||
|
|
||
| let newValue = detectable.isLockdownModeEnabled | ||
| guard newValue != self.isLockdownModeEnabled else { return } | ||
|
|
||
| Logger.navigation.debug("Lockdown Mode changed on foreground: \(newValue)") | ||
|
|
||
| // Re-run detection to update state and trigger sheet if needed | ||
| resetForForegroundCheck() | ||
| detectLockdownMode(for: detectable) | ||
|
|
||
| if shouldShowSheet { | ||
| presentSheetIfNeeded(onDismiss: {}) | ||
| } else { | ||
| // Lockdown Mode was disabled — dismiss the sheet if showing | ||
| dismissSheetIfPresented() | ||
| } | ||
| } | ||
|
|
||
| /// Presents the Lockdown Mode warning sheet if needed. | ||
| /// | ||
| /// - Parameters: | ||
| /// - onDismiss: Callback invoked when the user dismisses the sheet. | ||
| /// - Returns: `true` if the sheet was presented, `false` otherwise. | ||
| @discardableResult | ||
| public func presentSheetIfNeeded(onDismiss: @escaping () -> Void) -> Bool { | ||
| guard shouldShowSheet, let presentingViewController else { | ||
| return false | ||
| } | ||
|
|
||
| hasShownSheet = true | ||
| shouldShowSheet = false | ||
|
|
||
| let sheetView = LockdownModeSheet( | ||
| onDismiss: { [weak presentingViewController] in | ||
| guard let presentingViewController else { return } | ||
| presentingViewController.dismiss(animated: true) { | ||
| onDismiss() | ||
| } | ||
| }, | ||
| onLearnMore: { | ||
| // Open support article directly to the exclusion section using text fragment | ||
| if let url = URL(string: "https://support.apple.com/en-us/105120#:~:text=How%20to%20exclude%20apps%20or%20websites%20from%20Lockdown%20Mode") { | ||
| UIApplication.shared.open(url) | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| let hostingController = UIHostingController(rootView: sheetView) | ||
| hostingController.modalPresentationStyle = .pageSheet | ||
| hostingController.isModalInPresentation = true | ||
|
|
||
| if let sheet = hostingController.sheetPresentationController { | ||
| sheet.detents = [.large()] | ||
| sheet.prefersGrabberVisible = false | ||
| } | ||
|
|
||
| presentingViewController.present(hostingController, animated: true) | ||
| return true | ||
| } | ||
|
|
||
| /// Resets the monitor state to re-check Lockdown Mode status. | ||
| /// | ||
| /// Call this when the app returns from background to re-evaluate Lockdown Mode | ||
| /// and potentially show the sheet again if it's still enabled. | ||
| public func resetForForegroundCheck() { | ||
| hasShownSheet = false | ||
| } | ||
|
|
||
| /// Dismisses the sheet if it's currently presented. | ||
| /// | ||
| /// - Parameter completion: Optional callback invoked after dismissal completes. | ||
| public func dismissSheetIfPresented(completion: (() -> Void)? = nil) { | ||
| guard let presentingViewController, presentingViewController.presentedViewController != nil else { | ||
| completion?() | ||
| return | ||
| } | ||
|
|
||
| presentingViewController.dismiss(animated: false, completion: completion) | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.