View Protection API

Prev Next

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 blockExternalScreens and blockScreenshots configuration 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 id type. The header includes <objc/runtime.h> so that id is 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

prm_view_layer_protect_replace(orig, replace)

Marks the first provided CALayer as being hidden/invisible in screen captures. If provided, the second CALayer is updated to only be visible in screen captures. The first argument can also be nil if you only want to provide a layer to show.

prm_view_layer_reset(layer)

Resets the hide/show behavior of the provided CALayer.

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

prm_view_config_default()

Returns the default configuration as a struct prm_view_config.

prm_view_config_from_color(color)

Creates a struct prm_view_config using the provided UIColor as the solid color overlay.

prm_view_config_from_image(image)

Creates a struct prm_view_config using the provided UIImage as the image overlay.

prm_view_config_from_view(view)

Creates a struct prm_view_config using the provided UIView as the custom view overlay.

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

prm_view_protect(enable)

If enable is true, this function enables screen protection on all windows with the default configuration. Otherwise, removes any previously applied protection from all windows. Disabling does not affect layers that were protected with prm_view_layer_protect() or prm_view_layer_protect_with_config().

prm_view_protect_with_config(cfg)

Enables screen protection on all windows using the provided struct prm_view_config.

prm_view_layer_protect(layer, enable)

If enable is true, this function enables screen protection only for the provided CALayer using the default configuration. Otherwise, removes it from that layer.

prm_view_layer_protect_with_config(layer, cfg)

Enables screen protection only for the provided CALayer using the provided struct prm_view_config.

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