Skip to content

Commit

Permalink
fix: various issues with the preventing screenshots feature (#87)
Browse files Browse the repository at this point in the history
* fix: disable/enable methods iOS

* ci-fix: swiftlint

* ci-fix: removed variable w added mainWindow
  • Loading branch information
jonas-elias committed Mar 8, 2024
1 parent da3f25a commit 1074e69
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
53 changes: 28 additions & 25 deletions ios/Plugin/PrivacyScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import UIKit
private let plugin: PrivacyScreenPlugin
private let config: PrivacyScreenConfig
private let privacyViewController: UIViewController

private var isEnabled = false
private var window: UIWindow?
private var screenPrevent = UITextField()

init(plugin: PrivacyScreenPlugin, config: PrivacyScreenConfig) {
init(plugin: PrivacyScreenPlugin, config: PrivacyScreenConfig, window: UIWindow?) {
self.plugin = plugin
self.config = config
self.privacyViewController = PrivacyScreen.createPrivacyViewController(config: config)
Expand All @@ -18,20 +19,39 @@ import UIKit
if config.enable {
self.enable(completion: nil)
}

self.window = window
configurePreventionScreenshot()
}

public func configurePreventionScreenshot() {
guard let mainWindow = window else { return }

if !mainWindow.subviews.contains(screenPrevent) {
mainWindow.addSubview(screenPrevent)
screenPrevent.centerYAnchor.constraint(equalTo: mainWindow.centerYAnchor).isActive = true
screenPrevent.centerXAnchor.constraint(equalTo: mainWindow.centerXAnchor).isActive = true
mainWindow.layer.superlayer?.addSublayer(screenPrevent.layer)
if #available(iOS 17.0, *) {
screenPrevent.layer.sublayers?.last?.addSublayer(mainWindow.layer)
} else {
screenPrevent.layer.sublayers?.first?.addSublayer(mainWindow.layer)
}
}
}

@objc public func enable(completion: (() -> Void)?) {
self.isEnabled = true
DispatchQueue.main.async {
self.plugin.bridge?.webView?.disableScreenshots(imageName: self.config.imageName)
self.plugin.bridge?.webView?.disableScreenshots(imageName: self.config.imageName, screenPrevent: self.screenPrevent)
completion?()
}
}

@objc public func disable(completion: (() -> Void)?) {
self.isEnabled = false
DispatchQueue.main.async {
self.plugin.bridge?.webView?.enableScreenshots()
self.plugin.bridge?.webView?.enableScreenshots(screenPrevent: self.screenPrevent)
completion?()
}
}
Expand Down Expand Up @@ -85,29 +105,12 @@ import UIKit
}

public extension WKWebView {
// Cannot be tested in simulator
func disableScreenshots(imageName: String?) {
addSecureText(imageName)
addSecureText(imageName)
}

func addSecureText(_ imageName: String?) {
let field = UITextField()
field.isSecureTextEntry = true
field.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(field)
field.centerYAnchor.constraint(equalTo: self.topAnchor).isActive = true
field.centerXAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
self.layer.superlayer?.addSublayer(field.layer)
// Must be `last` for iOS 17, see https://github.com/capacitor-community/privacy-screen/issues/74
field.layer.sublayers?.last?.addSublayer(self.layer)
func enableScreenshots(screenPrevent: UITextField) {
screenPrevent.isSecureTextEntry = false
}

func enableScreenshots() {
for view in self.subviews {
if let textField = view as? UITextField {
textField.isSecureTextEntry = false
}
}
func disableScreenshots(imageName: String?, screenPrevent: UITextField) {
screenPrevent.isSecureTextEntry = true
}
}
2 changes: 1 addition & 1 deletion ios/Plugin/PrivacyScreenPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PrivacyScreenPlugin: CAPPlugin {

override public func load() {
let config = getPrivacyScreenConfig()
self.implementation = PrivacyScreen(plugin: self, config: config)
self.implementation = PrivacyScreen(plugin: self, config: config, window: UIApplication.shared.delegate?.window as? UIWindow)

NotificationCenter.default.addObserver(self, selector: #selector(self.handleDidBecomeActiveNotification),
name: UIApplication.didBecomeActiveNotification, object: nil)
Expand Down

0 comments on commit 1074e69

Please sign in to comment.