防止 iOS 应用程序中的屏幕捕获

问题描述 投票:0回答:9

出于安全原因,我需要防止我的应用程序的用户截屏。我显示的内容是机密的,不应复制到设备上。我在 Stack Overflow 上看到了一个答案,但对于 Android

在 iOS 中是否有可能以某种方式阻止屏幕捕获?

虽然通过点击几个按钮将屏幕截图捕获到图库中对用户来说是一个非常有用的功能,但也有一个有限的要求来防止它。有什么指示吗?

ios screenshot
9个回答
59
投票

我刚刚编写了 UIView 的简单扩展,允许将它从屏幕捕获、Airplay 镜像等中隐藏起来。该解决方案使用 UITextField 的功能来隐藏密码以防止捕获。

extension UIView {
    func makeSecure() {
        DispatchQueue.main.async {
            let field = UITextField()
            field.isSecureTextEntry = true
            self.addSubview(field)
            field.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
            field.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
            self.layer.superlayer?.addSublayer(field.layer)
            field.layer.sublayers?.first?.addSublayer(self.layer)
        }
    }
}

使用:

class ViewController: UIViewController {

   var secureView: UIView!

   override func viewDidLoad() {
      super.viewDidLoad()

      secureView.makeSecure()
   }
}

如果有人解释苹果是如何在内部实现这种魔法的,我将不胜感激。


30
投票

no 方法可以完全防止截屏。您可以做 Snapchat 所做的事情,即要求用户触摸屏幕以查看您正在显示的任何信息。这是因为系统截图事件中断了触摸。这不是一个完美的方法,您不能 100% 地阻止用户截屏。

更多细节:iOS 检测屏幕截图?


10
投票

已经有一段时间了,但我刚刚遇到ScreenShieldKit,这是消息应用程序 Confide 使用的一项正在申请专利的技术。它的作用是让用户截屏,但最终图片上的内容是空白的。他们最近发布了 iOS 版本。


6
投票

在移动到视图之前从视图中删除敏感信息 背景。当应用程序过渡到后台时, 系统拍摄应用程序主窗口的快照,它 然后在将您的应用程序转换回 前景。在从您的applicationDidEnterBackground返回之前: 方法,您应该隐藏或隐藏密码和其他敏感信息 可能作为快照的一部分捕获的个人信息。

在 swift 4 中将此代码添加到您的应用委托中。

在应用程序委托中声明一个变量

var imageview : UIImageView?

func applicationWillResignActive(_ application: UIApplication) {

        imageview = UIImageView.init(image: UIImage.init(named: "bg_splash"))
        self.window?.addSubview(imageview!)
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

func applicationDidBecomeActive(_ application: UIApplication) {
        if (imageview != nil){

            imageview?.removeFromSuperview()
            imageview = nil
        }
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

3
投票

这是一个很棒的 Swift 包,用于从系统屏幕截图和视频录制中隐藏视图:SnapshotSafeView


2
投票

我听说您可以使用 UIApplicationUserDidTakeScreenshotNotification 监听屏幕截图事件

 NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationUserDidTakeScreenshotNotification
                                                  object:nil
                                                   queue:mainQueue
                                              usingBlock:^(NSNotification *note) {
                                                  // executes after screenshot
                                                  NSLog(@"Screenshot Detection : %@", note);
                                                  UIAlertView *screenshotAlert = [[UIAlertView alloc] initWithTitle:@"Screenshot Detected" message:@"Oh Oh no screenshot bruhh" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                                                  [screenshotAlert show];
                                              }];

如果您可以在制作截图文件时立即将其删除怎么办?


1
投票

您可以在下面使用此扩展程序。请将此方法应用于父视图

   extension UIView {
    
    func preventScreenshot(for view: UIView) {
        let textField = UITextField()
        textField.isSecureTextEntry = true
        textField.isUserInteractionEnabled = false
        guard let hiddenView = textField.layer.sublayers?.first?.delegate as? UIView else {
            return
        }
        
        hiddenView.subviews.forEach { $0.removeFromSuperview() }
        hiddenView.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(hiddenView)
        hiddenView.fillSuperview()
        hiddenView.addSubview(view)
    }
}

所以例如能够防止在 scrollView 上截屏

private weak var scrollView: UIScrollView! (it's an outlet)

在您的viewDidLoad中,只需在下面执行此操作

self.view.preventScreenshot(for: self.scrollView)

注意:fillSuperview 只是将您的视图锚定到其父视图,因此如下所示:

NSLayoutConstraint.activate([
        hiddenView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
        hiddenView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
        hiddenView.bottomAnchor.constraint(equalTo: self.bottomAnchor),
        hiddenView.topAnchor.constraint(equalTo: self.topAnchor)
    ])

0
投票

你可以使用这个工具。它从屏幕截图中隐藏了敏感信息。

https://screenshieldkit.com


0
投票

我用下面这个包防止自己生成的二维码截图

https://github.com/shvul/PrivateImage

到目前为止,我已经能够毫无问题地使用它。

© www.soinside.com 2019 - 2024. All rights reserved.