Privacy shield for Cocoa apps

Apptics' privacy shield prevents the unauthorized capture of sensitive information such as financial information, personal information, private photos, and so on. Once added to the app, the privacy shield will detect the screenshots or screen recording in the app and immediately hide the sensitive content. This helps you to keep your private information, private.

Requirements

  • Applicable for apps with iOS 12+ and iPadOS 13+.

Installation 

  • Edit the podfile to install the required library for Apptics privacy shield.
Copiedsource 'https://github.com/CocoaPods/Specs.git'
target '[TARGET NAME]' do
	pod 'AppticsPrivacyShield'
end

Secure the app views

  • Wrap the app views in the Secure Views as given in the example below.
Copiedimport UIKit
import AppticsPrivacyShield

class ViewController: UIViewController {
    
// Apptics's secure views
let sImageView = APSecureImageView()
let sSecureButton = APSecureButton()
let sSecureLabel = APSecureLabel()
let sSecureTextView = APSecureTextView()
    
override func viewDidLoad() {
	super.viewDidLoad()    
	// Add to subview here.
    
	}

	// To enable or disable screen capture, recording, or sharing
	func setPreventScreenCapture(_ shouldPrevent : Bool){
 		sSecureButton.secureContainer.preventScreenCapture = shouldPrevent
 		sSecureLabel.secureContainer.preventScreenCapture = shouldPrevent
  	sSecureTextView.secureContainer.preventScreenCapture = shouldPrevent
  	sImageView.secureContainer.preventScreenCapture = shouldPrevent
 }
}

Customize the app view

  • Customize the app views as given below.
CopiedsSecureLabel.text = "Private Information"
let customLabel = sSecureLabel.label
customLabel.textColor = .white
customLabel.textAlignment = .center
customLabel.layer.cornerRadius = 10
customLabel.layer.masksToBounds = true
sSecureButton.button.setTitle("Secure Button", for: .normal)
sSecureButton.button.setTitleColor(.white, for: .normal)
sSecureButton.button.backgroundColor = .systemBlue
sSecureButton.button.layer.cornerRadius = 10

Protect entire view

  • Protect the entire view using the below code.
Copiedclass SecureViewController: UIViewController {
let tableView = UITableView()
lazy var screenshotPreventView = APSecureView(contentView: tableView)

override func viewDidLoad() {
	super.viewDidLoad()
		// As a default behaviour, SDK will hide the view. To disable it, please set the below value to false.
		view.addSubview(screenshotPreventView)
		screenshotPreventView.isUserInteractionEnabled = true
      screenshotPreventView.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
                    screenshotPreventView.leadingAnchor.constraint(equalTo:	view.safeAreaLayoutGuide.leadingAnchor),
                    screenshotPreventView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
                    screenshotPreventView.topAnchor.constraint(equalTo: view.topAnchor),
                    screenshotPreventView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
 		 ])
	}

	// To enable or disable screen capture, recording, or sharing
	func setPreventScreenCapture(_ shouldPrevent : Bool){
  	screenshotPreventView.preventScreenCapture = shouldPrevent
 }
}

Protect window from screen sharing

  •  Protect the entire window from screen sharing using the below code.
Copiedoverride func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
           
	 APWindowShield.shared().enableScreenRecordingMonitoring()
}
    
override func viewDidDisappear(_ animated: Bool) {
		super.viewDidDisappear(animated)
     APWindowShield.shared().disableScreenRecordingMonitoring()

}

Note: APWindowShield will protect the window from screen sharing and recording by adding a customizable message view over your window.

Customize the message view

  • Customize the message view as shown in the below example.
Copiedoverride func viewDidLoad() {
 	super.viewDidLoad()
           
		// Create and configure the label
		let label = UILabel()
 		label.text = "This screen is secured"
 		label.font = UIFont.boldSystemFont(ofSize: 20)
 		label.textColor = UIColor.black
		label.translatesAutoresizingMaskIntoConstraints = false
		// Assign the label to window shield 
 		APWindowShield.shared().label = label
        
 		let imageView = UIImageView()
 		let image = UIImage(systemName: "eye.slash.fill")
 		imageView.image = image
 		imageView.contentMode = .scaleAspectFit
 		imageView.translatesAutoresizingMaskIntoConstraints = false
		// Assign the imageView to window shield 
 		APWindowShield.shared().imageView = imageView

 		// Or you could add your custom message view 
		let customView = UIView()
        
       // Create and configure the label
       let label = UILabel()
       label.text = "This screen is secured!"
       label.font = UIFont.boldSystemFont(ofSize: 20)
       label.textColor = UIColor.black
       label.translatesAutoresizingMaskIntoConstraints = false

       // Create and configure the image view
       let imageView = UIImageView()
       let image = UIImage(systemName: "eye.slash.fill")
       imageView.image = image
       imageView.contentMode = .scaleAspectFit
       imageView.translatesAutoresizingMaskIntoConstraints = false

       // Add subviews to the blur view
       customView.addSubview(label)
       customView.addSubview(imageView)

       // Define and activate layout constraints
       NSLayoutConstraint.activate([
            label.centerXAnchor.constraint(equalTo: customView.centerXAnchor),
            label.centerYAnchor.constraint(equalTo: customView.centerYAnchor, constant: -30),
            imageView.centerXAnchor.constraint(equalTo: customView.centerXAnchor),
            imageView.bottomAnchor.constraint(equalTo: label.topAnchor, constant: -10),
            imageView.widthAnchor.constraint(equalToConstant: 100),
            imageView.heightAnchor.constraint(equalToConstant: 100)
       ])
       // Assign the customview to window shield 
 		 APWindowShield.shared().contentView = customView
 		 APWindowShield.shared().enableScreenRecordingMonitoring()
}

Screenshot monitoring

Start monitoring for screenshots

Use the method below to enable screenshot monitoring. When this method is called, the user will be prompted to temporarily disable the Privacy Shield to allow screenshots. To grant permission, the user should authenticate using either the biometric data (e.g., FaceID or TouchID) or the device password.

Copied[AppticsPrivacyShield startMonitoring];
CopiedAppticsPrivacyShield.startMonitoring()

Stop monitoring for screenshots

Use the method below to disable the screenshot monitoring. No alerts or prompts will be shown to the user. This method will stop the monitoring functionality.

Copied[AppticsPrivacyShield stopMonitoring];
CopiedAppticsPrivacyShield.stopMonitoring()

Enable or disable the privacy shield

The below method will allow you to programmatically enable or disable the screenshot detection and alert feature.

Note: Privacy shield is always active to protect the sensitive data. The below method controls whether screenshot detection and user alerts are enabled.

Copied[AppticsPrivacyShield enablePrivacyShield:YES];
CopiedAppticsPrivacyShield.enable(true)

Use the method below to check whether Apptics privacy shield is currently enabled or disabled. The method will return a boolean value indicating the current status of the privacy shield. 

Copied[AppticsPrivacyShield privacyShieldStatus];
CopiedAppticsPrivacyShield.privacyShieldStatus()

Note: To enable screenshot monitoring and request permission for screenshots, you must include NSFaceIDUsageDescription key-value pair in the app's info.plist file. This is necessary to handle the biometric or password authentication prompt when temporarily disabling privacy shield for screenshots.