-
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| 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 } | ||
|
|
||
| /// Reloads the content if supported by this object. | ||
| func reloadForLockdownMode() | ||
| } | ||
|
|
||
| /// Extension to make WKWebView conform to LockdownModeDetectable. | ||
| extension WKWebView: LockdownModeDetectable { | ||
| var isLockdownModeEnabled: Bool { | ||
| configuration.defaultWebpagePreferences.isLockdownModeEnabled | ||
| } | ||
|
|
||
| // WKWebView.reload() returns WKNavigation?, which doesn't satisfy the | ||
| // Void-returning protocol requirement. This wrapper discards the result. | ||
| func reloadForLockdownMode() { | ||
| _ = reload() as WKNavigation? | ||
| } | ||
| } | ||
|
|
||
| /// 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 reloading on foreground. | ||
| private weak var detectable: LockdownModeDetectable? | ||
|
|
||
| /// Callback invoked when the editor needs to reset its ready state. | ||
| private var onResetReadyState: (() -> Void)? | ||
|
|
||
| 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. | ||
| /// | ||
| /// - Parameters: | ||
| /// - viewController: The view controller to use for sheet presentation. | ||
| /// - onResetReadyState: Callback invoked when the editor should reset its ready state. | ||
| public func setup( | ||
| presentingViewController viewController: UIViewController, | ||
| onResetReadyState: @escaping () -> Void | ||
| ) { | ||
| self.presentingViewController = viewController | ||
| self.onResetReadyState = onResetReadyState | ||
|
|
||
| // Observe foreground notifications to re-check Lockdown Mode | ||
| NotificationCenter.default.addObserver( | ||
| self, | ||
| selector: #selector(handleWillEnterForeground), | ||
| name: UIApplication.willEnterForegroundNotification, | ||
| object: nil | ||
| ) | ||
| } | ||
|
|
||
| @objc private func handleWillEnterForeground() { | ||
| Logger.navigation.debug("Will enter foreground") | ||
|
|
||
| // Always re-check on foreground to detect Lockdown Mode state changes. | ||
| // This handles both: | ||
| // 1. User excluded app from Lockdown Mode while in Settings (enabled -> disabled) | ||
| // 2. User enabled Lockdown Mode for app while in Settings (disabled -> enabled) | ||
|
|
||
| // Reset the monitor to allow re-detection and potentially show sheet again | ||
| resetForForegroundCheck() | ||
|
|
||
| // Reset the editor's ready state so it goes through loading flow again | ||
| onResetReadyState?() | ||
|
|
||
| // Dismiss the sheet if presented, then reload to re-run detection | ||
| dismissSheetIfPresented { [weak self] in | ||
| // Reload triggers navigation delegate which calls detectLockdownMode() | ||
| // If Lockdown Mode state changed: | ||
| // - Now enabled: sheet will be shown | ||
| // - Now disabled: editor will fade in normally without sheet | ||
| self?.detectable?.reloadForLockdownMode() | ||
| } | ||
| } | ||
|
|
||
| /// 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 = [.medium()] | ||
| sheet.prefersGrabberVisible = false | ||
| } | ||
|
|
||
| presentingViewController.present(hostingController, animated: true) | ||
|
Comment on lines
+167
to
+176
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm unsure what leads to this, but the sheet is not all that accessible via VoiceOver. When it opens, the focus is moved to the status bar instead of the sheet itself. Afterwards, you cannot swipe to move focus to the sheet; you can only tap or drag your finger atop the sheet to begin reading its contents. |
||
| 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 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure this is an acceptable trade-off for ensuring the editor state is always accurate regarding Lockdown Mode. This results in the editor always reloading when foregrounded, regardless of whether Lockdown Mode is modified, enabled, or disabled. This feels like a significant downgrade in the UX.
At best, the editor flashes while reloading and loses any transient state (open modals, popover placement, text selection, etc). At worst, content is loss. Presumably the latter will not occur if proper content syncing occurs in the host app, but still...
Editor reload flash
ScreenRecording_04-03-2026.08-16-08_1.MP4
Instead of reloading, should we consider updating the copy to include something like "and re-open the editor" after excluding the app?
What are your thoughts on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noting that Jeremy and I discussed this some on a call...
The content loss in the iOS demo app likely occurs because the demo app does not implement a persistence layer alongside using the library's
editorDidRequestLatestContentfunction—WordPress-iOS does.Regardless, reloading the editor on every foreground is a bug; it should only reload when Lockdown Mode status changed.
We should address the unexpected reload, or replace the automatic reload with a note directing the user to re-open the editor.