ios 如何防止实际的UI内容出现在屏幕截图中?

p1tboqfb  于 2023-10-21  发布在  iOS
关注(0)|答案(1)|浏览(121)

我需要做的操作在这里是限制与其他一些用户界面的实际用户界面时,用户试图采取屏幕截图,但在这里,当我试图采取屏幕截图的红色块视图出现,但保存的图像有实际的用户界面内容如何克服这一点。
有没有人可以帮助我实现这一点,比如你可能看到任何支付或安全应用程序不允许屏幕截图,但在这里它需要被允许,但我们需要覆盖实际的用户界面呈现在partucular视图。

class HomeViewController: UIViewController {
    
    private var greetLabel: UILabel!
    
    private let navigator: HomeNavigator
    
    init(navigator: HomeNavigator) {
        self.navigator = navigator
        super.init(nibName: nil, bundle: nil)
        setupTabBar()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupNavigationBar()
        setupViews()
        setupConstraints()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        NotificationCenter.default.addObserver(self, selector: #selector(handleScreenshot), name: UIApplication.userDidTakeScreenshotNotification, object: nil)
      }
      
      override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
          NotificationCenter.default.removeObserver(self, name: UIApplication.userDidTakeScreenshotNotification, object: nil)
      }
      
   
    @objc func handleScreenshot() {
        let blockerView = ScreenshotBlockerView(frame: UIScreen.main.bounds)
        if let keyWindow = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) {
            keyWindow.addSubview(blockerView)
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            self.removeScreenshotBlockerView()
        }
    }
    
    
    func removeScreenshotBlockerView() {
        if let keyWindow = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) {
            for subview in keyWindow.subviews {
                if subview is ScreenshotBlockerView {
                    subview.removeFromSuperview()
                }
            }
        }
    }
    
    private func setupNavigationBar() {
        navigationItem.title = "Home"
        self.navigationItem.largeTitleDisplayMode = .never
        navigationController?.navigationBar.prefersLargeTitles = true
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "",style: .plain,target: nil,action: nil)
    }
    
    private func setupTabBar() {
        let tabBarItem = UITabBarItem(title: "Home", image: UIImage(systemName: "house") , tag: 0)
        tabBarItem.selectedImage = UIImage(systemName: "house.fill")
        self.tabBarController?.tabBar.barTintColor =  UIColor.blue
        self.tabBarItem = tabBarItem
    }
    
    private func setupViews() {
        view.backgroundColor = .white
        
        let greetLabel = UILabel()
        greetLabel.numberOfLines = 0
        greetLabel.font = UIFont.systemFont(ofSize: 18 , weight: UIFont.Weight.semibold)
        greetLabel.textColor = UIColor.black
        greetLabel.translatesAutoresizingMaskIntoConstraints = false
        greetLabel.text = AppConstants.shared.greetLabelText(forType: .itemConstant)
        
        self.greetLabel = greetLabel
        
        view.addSubview(greetLabel)
        
    }
    
    private func setupConstraints() {
        NSLayoutConstraint.activate([
            self.greetLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            self.greetLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        ])
    }
}

class ScreenshotBlockerView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.red
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
}
bf1o4zei

bf1o4zei1#

AFAIK目前这是不可能的。在实际截图发生之前,我们无法接收事件,我们可以做出React。截图后只有一个事件。
https://screenshieldkit.com/我读到这家公司有一个工作解决方案,但它需要付款。他们有一个自定义的UI组件,可以在截图时隐藏视图。他们通过某种方式将视图转换为带有DRM的视频

相关问题