Created
August 17, 2022 04:04
-
-
Save yoxisem544/bed56d4aded6697a6163e5db314e6e17 to your computer and use it in GitHub Desktop.
Prevent screenshot view wrapper
This file contains 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
import UIKit | |
final class ScreenshotPreventingView: UIView { | |
// MARK: - π Constants | |
// MARK: - πΆ Properties | |
var preventScreenCapture = true { | |
didSet { | |
textField.isSecureTextEntry = preventScreenCapture | |
} | |
} | |
private var contentView: UIView? | |
private let textField = UITextField() | |
private let recognizer = HiddenContainerRecognizer() | |
private lazy var hiddenContentContainer: UIView? = try? recognizer.getHiddenContainer(from: textField) | |
// MARK: - π¨ Style | |
// MARK: - 𧩠Subviews | |
// MARK: - π Actions | |
// MARK: - π¨ Initialization | |
init(contentView: UIView) { | |
self.contentView = contentView | |
super.init(frame: .zero) | |
setupUI() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
// MARK: - πΌ View Lifecycle | |
// MARK: - π UI | |
private func setupUI() { | |
textField.backgroundColor = .clear | |
textField.isUserInteractionEnabled = false | |
guard let container = hiddenContentContainer else { return } | |
addSubview(container) | |
container.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate([ | |
container.leadingAnchor.constraint(equalTo: leadingAnchor), | |
container.trailingAnchor.constraint(equalTo: trailingAnchor), | |
container.topAnchor.constraint(equalTo: topAnchor), | |
container.bottomAnchor.constraint(equalTo: bottomAnchor) | |
]) | |
guard let contentView = contentView else { return } | |
setup(contentView: contentView) | |
DispatchQueue.main.async { | |
// setting secure text entry in init block will fail | |
// setting default value inside main thread | |
self.preventScreenCapture = true | |
} | |
} | |
// MARK: - π Public Methods | |
func setup(contentView: UIView) { | |
self.contentView?.removeFromSuperview() | |
self.contentView = contentView | |
guard let container = hiddenContentContainer else { return } | |
container.addSubview(contentView) | |
contentView.translatesAutoresizingMaskIntoConstraints = false | |
let bottomConstraint = contentView.bottomAnchor.constraint(equalTo: container.bottomAnchor) | |
bottomConstraint.priority = .required - 1 | |
NSLayoutConstraint.activate([ | |
contentView.leadingAnchor.constraint(equalTo: container.leadingAnchor), | |
contentView.trailingAnchor.constraint(equalTo: container.trailingAnchor), | |
contentView.topAnchor.constraint(equalTo: container.topAnchor), | |
bottomConstraint | |
]) | |
} | |
// MARK: - π Private Methods | |
} | |
// MARK: - π§Ά Extensions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment