9

I want to prevent taking screenshot of a page in app. how to do it programmatically so that screenshots cannot be taken.

Found code to detect screenshot. Can it be deleted as soon as a screenshot is taken?

let mainQueue = NSOperationQueue.mainQueue()
NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationUserDidTakeScreenshotNotification,
    object: nil,
    queue: mainQueue) { notification in
        // executes after screenshot
}
Idan
  • 5,405
  • 7
  • 35
  • 52
Thripthi Haridas
  • 353
  • 1
  • 5
  • 18
  • there is no way of doing that, you can check with jailbrake – Lu_ Jan 09 '17 at 09:25
  • 1
    If you want the user to not be able to take screenshot , because of sensitive information displayed on that certain page, you should try and do it another way because it's not such a good practice, even in @Jacob King's answer – Mr. Xcoder Jan 09 '17 at 09:32
  • thanks everybody for the reply – Thripthi Haridas Jan 09 '17 at 10:47
  • there is an SO question about it: https://stackoverflow.com/questions/18680028/prevent-screen-capture-in-an-ios-app – wzso Aug 08 '18 at 01:43
  • It is possible, the Amazon Prime Video app does that, if you want to make a screenshot, you will get only a black rectangle. Sorry, I don't know how to do that programmatically, but it is possible without access to the photo gallery. – Alex Jul 22 '17 at 13:28

4 Answers4

10

There is no way to prevent ScreenShots but you can prevent Screen Recording through this code.

func detectScreenRecording(action: @escaping () -> ()) {
    let mainQueue = OperationQueue.main
    NotificationCenter.default.addObserver(forName: UIScreen.capturedDidChangeNotification, object: nil, queue: mainQueue) { notification in
        // executes after screenshot
        action()
    }
}
        
        
        
//Call in vewWillApper 
detectScreenRecording {
    print(UIScreen.main.isCaptured)
    if UIScreen.main.isCaptured {
        //your vier hide code
        print("self.toHide()")
    } else {
        // self.sceneDeleg(ate?.window?.isHidden = false
        //your view show code
        print("self.toShow()")
    }
}
Dmytro Rostopira
  • 10,588
  • 4
  • 64
  • 86
3

There is absolutely no way to completely prevent user from taking screenshot during the app process, and that's because you do not have access to delete photos in the photo gallery of the user. It would totally be a security issue if you could access your user's photos.


However, there are ways to partially prevent screenshots, as described here: Prevent screen capture in an iOS app

Community
  • 1
  • 1
Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44
0

Technically that is possible, via the Photos framework, the docs for which can be found here.

Example code can be found here.

However, this will ask the user's permission first, and then again to confirm deletion; so possibly not the ideal solution. Unfortunately this is as good as it gets as Apple has the Camera Roll fairly locked down.

Community
  • 1
  • 1
Jacob King
  • 6,025
  • 4
  • 27
  • 45
0

You cannot prevent user from taking screenshot, however, you can hide the content while a screenshot is taken, Use this code to do so..

   extension UIView {
    func hideContentOnScreenCapture() {
        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)
        }
    }
}

Usage:

yourView.hideContentOnScreenCapture()

Akash Neeli
  • 337
  • 4
  • 12