The App Shielding configuration options Block external screens and Block screenshots protect the entire app from screen captures at all times. With the App Shielding SDK’s View Protection API, screen capture protection can be dynamically enabled/disabled, customized with your own colors or images, and applied to specific layers.
To avoid unwanted behavior, the
blockExternalScreensandblockScreenshotsconfiguration options should be disabled if any protections are enabled through the API.
To use the API, you must add the SDK/C/ShieldSDK.xcframework bundle from the Shield package to your project and include the ShieldSDK/view_protection.h header. The API enables the following protection techniques:
Hide a specific layer from screen captures
Show a specific layer during screen captures
Protect all windows with a configured overlay
Protect a specific layer with a configured overlay
The View Protection API operates on UIKit objects, which are passed as the Objective-C
idtype. The header includes<objc/runtime.h>so thatidis available in plain C, but the layers, colors, images, and views themselves must still come from UIKit.
Hide and Show Layers
The preferred way to use the View Protection API is to hide a given layer from screen captures and/or show a different layer. This is accomplished with the following functions:
Function | Description |
|---|---|
| Marks the first provided |
| Resets the hide/show behavior of the provided |
These functions must be called on the main UI thread.
Example
The following example hides the given label from screen captures and replaces it with a different image:
UIImage *privacyImage = [UIImage imageNamed:@"security_overlay"];
UIImageView *privacyView = [[UIImageView alloc] initWithImage:privacyImage];
UILabel *label = [[UILabel alloc] init];
label.text = @"Sensitive Information";
prm_view_layer_protect_replace(label.layer, privacyView.layer);For the replacement to look correct, the two layers must cover the same part of the screen.
Custom Overlays
Custom screen capture overlays can be applied to all windows or individual, specific layers. If applied to all windows, protection only takes into account windows that exist at the time the enable function is called. Any new windows created after calling the function are not automatically protected.
Custom overlays are an experimental feature and might not always work reliably.
Configurations
Configured overlays can be one of the following:
A solid color. The default configuration uses solid white if the system theme is set to light mode and solid black for dark mode.
An image. The image is scaled to cover the entire protected area.
A view. The view is added to the view hierarchy when protection is enabled.
Configurations are values of type struct prm_view_config, which are created with the following functions:
Functions | Description |
|---|---|
| Returns the default configuration as a |
| Creates a |
| Creates a |
| Creates a |
The configuration does not take ownership of the corresponding UIKit object, so the object must still be valid when you pass the configuration to one of the protection functions described below.
Protection Functions
Custom protections are applied with the following functions:
Function | Description |
|---|---|
| If |
| Enables screen protection on all windows using the provided |
| If |
| Enables screen protection only for the provided |
These functions must be called on the main UI thread.
It is recommended to enable view protection once when the app starts, not repeatedly in response to events like screen recording detection or view lifecycle methods. For example:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
prm_view_protect(true);
return YES;
}Examples
The following example applies a custom view to all windows:
#include <ShieldSDK/view_protection.h>
...
// Create configuration
UIView *customOverlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
customOverlay.backgroundColor = [UIColor redColor];
struct prm_view_config viewConfig = prm_view_config_from_view(customOverlay);
// Enable protection
prm_view_protect_with_config(viewConfig);The following example applies a custom image to a specific layer:
UIImage *warningImage = [UIImage imageNamed:@"warning_overlay"];
struct prm_view_config imageConfig = prm_view_config_from_image(warningImage);
prm_view_layer_protect_with_config(self.passwordField.layer, imageConfig);