How to detect a screenshot on iOS in order to block the content (similar to WhatsApp View Once photos)
Asked Answered
T

2

7

I am trying to mimic what WhatsApp is doing when taking a screenshot of View Once photos, which is to add an overlay to the content that is sensitive (see image below)

From what I see in the documentation, we only get a notification after the screenshot is taken.

I wonder how they did it to detect when the screenshot is taken to add this overlay

whatsapp blocks sensitive content when taking a screenshot

Finally, if the user tries to record the screen, I think this notification is enough to add the overlay, however I don't know how to do it when they take a screenshot.

Tomika answered 2/6, 2023 at 11:41 Comment(2)
You could use a solution I posted here, i think it's pretty handy: #68422211Meister
Does this answer your question? Prevent screen capture in an iOS appBissonnette
T
2

I think the best solution is to use this extension to block the screenshots.

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)
        }
    }
}

How to use it:

let yourView = UIView()
yourView.makeSecure()

It will prevent the user from taking a screenshot or recording the specific view.

If you need to show a placeholder when a user tries to screenshot or record then check this repo.

Sample code: https://github.com/kuttz/SecureYourView

Result: https://github.com/kuttz/DemosAndScreenShots/blob/main/SecureYourView/Demo.gif

Telephony answered 2/6, 2023 at 14:5 Comment(4)
Not working at allCoccidioidomycosis
Did you try the sample project?Telephony
Yes, it does not work and crashes after a while due to an infinite loop. I think it's because you are adding the field's layer as a subview of the self and then the self's layer as a sublayer of the fieldCoccidioidomycosis
Above SecureYourView is working well for meCacique
B
0

I worked on an app where securing chat content was a requirement. After R&D, I concluded that preventing screen capture in an iOS app is not directly supported by the iOS operating system. However, we can customize our code to achieve the desired result.

As a solution, I created ScreenShield, an iOS library designed to secure content by blocking screenshots and screen recordings. It provides a simple solution to protect your app's content. I hope it proves helpful for others facing the same challenge.

ScreenShield for ios UIKit/SwiftUI

HOW TO USE:

Add the following line to your Podfile and run pod install:

pod 'ScreenShield'

UIKIT EXAMPLE:

import UIKit
import ScreenShield

class ViewController: UIViewController {
    
    override func viewDidAppear() {
        super.viewDidAppear()
        
        // Protect ScreenShot
        ScreenShield.shared.protect(view: self.view)
        
        // Protect Screen-Recording
        ScreenShield.shared.protectFromScreenRecording()
    }
}

SWIFTUI EXAMPLE:

    import SwiftUI
import ScreenShield

struct ContentView: View {
    
    var body: some View {
        Text("Hello, World!")
            .protectScreenshot() // Protect the view
            .onAppear {
                ScreenShield.shared.protectFromScreenRecording() // Protect Screen-Recording
            }
    }
}
Bissonnette answered 29/1 at 18:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.