Callbacks API

Prev Next

When App Shielding performs security checks, it can either be configured to exit the application when a security problem is detected (e.g., Exit on hooking frameworks), or it can be configured to notify the application via a callback. By handling the callback, you can, for instance, inform the user about the possible security threat.

Delegate callbacks currently occur when the application registers an observer with PRMShieldEventManager, during application launch, and when the application is moved to the foreground.

To implement callbacks

  1. Ensure the relevant security feature is enabled in the configuration (e.g., Check jailbreak), and is not configured to exit the application when the security problem is detected (e.g., Exit on jailbreak).

  2. Ensure that SDK/ObjC/ShieldSDK.xcframework is included in the project.

  3. Import the ShieldSDK/Shield.h umbrella header into any Objective-C source files that will interact with the callback APIs.

  4. Create a class that implements the PRMShieldEventDelegate protocol, as defined in ShieldEvent.h. This protocol defines delegate methods that, when implemented, are called when different security checks are finished.

    The delegate methods are triggered on the main thread, so it is possible to perform UI operations directly from the delegate methods.

  5. Register an object of the class implementing the PRMShieldEventDelegate protocol by calling the - addObserver method of the PRMShieldEventManager class. This registration should be performed as early as possible, because many security checks are performed right after application has launched. Ideally, the registration is performed in the didFinishLaunchingWithOptions method of the application delegate.

    Observers can also be removed with the removeObserver method of the PRMShieldEventManager API. However, App Shielding indefinitely retains observers that were added with the legacy ShieldCallbackManager API. They cannot be removed, and their callbacks remain active for as long as the application is running.

Example implementations

The following examples register the observer and handle the callbacks for jailbreak and screenshot checks. Note that some callbacks, like jailbreakStatus, are always triggered and provide a boolean value that indicates whether the security check is positive. Other callbacks, like screenshotDetected, are only triggered when the offending event is detected and thus provide no such value.

Objective-C

The following example uses Objective-C:

#import "AppDelegate.h"

#import <ShieldSDK/Shield.h>

@interface AppDelegate () <PRMShieldEventDelegate>
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[PRMShieldEventManager sharedManager] addObserver:self];

    if (!(@available(iOS 16, *)))
        NSLog("%@", @"Developer mode status checking unavailable pre-iOS 16.");

    return YES;
}

- (void)jailbreakStatus:(BOOL)isJailbroken
{
    if (isJailbroken)
        [self handleDeviceIsJailbroken];
}

- (void)screenshotDetected
{
    [self handleScreenshotDetected];
}

@end

Swift

The following example uses Swift:

// AppDelegate.swift

import UIKit
import ShieldSDK

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, PRMShieldEventDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        PRMShieldEventManager.shared.add(observer: self)
        return true
    }

    func jailbreak(status isJailbroken: Bool) {
        if isJailbroken {
            handleDeviceIsJailbroken()
        }
    }

    func screenshotDetected() {
        handleScreenshotDetected()
    }
}

Swift from Framework

The following example also uses Swift but in the context of a framework:

// Callbacks.swift

import ShieldSDK

internal class Callbacks: NSObject, PRMShieldEventDelegate {

    func start()  {
        PRMShieldEventManager.shared.add(observer: self)

        if #unavailable(iOS 16) {
            print("Developer mode status checking unavailable pre-iOS 16.")
        }
    }

    func jailbreak(status isJailbroken: Bool)  {
        if isJailbroken {
            handleDeviceisJailbroken()
        }
    }

    func screenshotDetected() {
        handleScreenShotDetected()
    }
}

This is largely the same code as the previous Swift example. However, a framework usually doesn’t implement the main AppDelegate. This means it cannot easily override didFinishLaunchingWithOptions, where you would normally call PRMShieldEventManager.shared.add(observer:).

One solution is to have the app call a function in the framework from didFinishLaunchingWithOptions, which can then call Callbacks.start(). For example:

// MyFramework.swift

private let callbacks = Callbacks()

public func frameWorkInit() {
    callbacks.start()
}

Of course, any other way of calling the setup as early as possible will work, as well. For example, you can use an Objective-C +load static constructor, a C++ static initializer, or a C __attribute__(constructor).

List of callbacks on iOS

The following methods are called after App Shielding has performed its security checks:

Callback

Description

jailbreakStatus

Provides a boolean that indicates whether the device on which the application is running is jailbroken.

Some detectors for Dopamine RootHide jailbreaks currently do not trigger a callback.

systemIntegrityProtectionStatus

Provides a boolean that indicates whether the app is running on a Mac with System Integrity Protection (SIP) disabled.

repackagingStatus

Provides a boolean that indicates whether the application has been repackaged. If the application is running in a simulator, this method is never called.

screenRecordingStatus

Provides a boolean that indicates whether one or more screens used by the app is being recorded.

developerModeStatus

Provides a boolean that indicates whether Developer Mode is enabled on the device. Developer Mode was added in iOS 16, so this method always returns false on devices running iOS 15 and earlier.

It is never called on non-iOS devices (e.g., tvOS).

debuggerStatus

Provides a boolean that indicates whether a debugger is attached to the host application. If App Shielding is configured to block debuggers, this method is never called.

screenshotDetected

Called when App Shielding discovers that a screenshot of the application has been taken. If the application is running in a simulator, this method is never called.

libraryInjectionPrevented

Called when App Shielding has prevented an untrusted library from being injected into the process during runtime.

This callback is deprecated and does not trigger on iOS/tvOS 17 or later and macOS 14 or later. Instead, the app terminates unexpectedly.

libraryInjectionDetected

Called when App Shielding has detected that there are untrusted libraries in the process. A repackaged app can also trigger this callback in addition to the repackaging callback. In production, however, you should configure App Shielding to use the Exit On Library Injection option, and not rely on handling a callback.

hookingFrameworksDetected

Called when App Shielding has detected that there are hooking frameworks in the process. In production, however, you should configure App Shielding to use the Exit On Hooking Frameworks option and not rely on handling a callback.

Exit on... security options like Exit on Jailbreak will cause the application to immediately exit before the corresponding callback is triggered. Also note that some of these options, like Exit on Repackaging, are always enabled in release mode.

Debugging: manual triggering of callbacks

For debugging purposes, each callback can be manually triggered using singleton methods of the PRMShieldEventManager class. In Objective-C, this looks like the following example:

#import <ShieldSDK/ShieldEventManager+TriggerEvent.h>
...

#ifdef DEBUG
PRMShieldEventManager *manager = [PRMShieldEventManager sharedManager];
[manager triggerJailbreakStatus:YES afterDelay:2];
#endif

In Swift, triggers are used in the following manner:

import ShieldSDK
...

#if DEBUG
PRMShieldEventManager.shared.triggerJailbreakStatus(true, afterDelay: 2)
#endif

In both examples, the jailbreakStatus callback is triggered with a status of true (i.e., the device is jailbroken) after a delay of two seconds. To immediately trigger the callback, set afterDelay to 0.

Callback triggers are designed for debugging purposes only and should be conditionally applied to debug builds or removed from the app code entirely before running the Shielding Tool on the app. The app will terminate unexpectedly if callback triggers are present in the App Shielding-protected version of the app.

Available callback triggers

PRMShieldEventManager provides an API with the following methods that accept a boolean status value as the first argument and an afterDelay interval as the second argument:

- (void)triggerJailbreakStatus:(BOOL)status afterDelay:(NSTimeInterval)delay;
- (void)triggerRepackagingStatus:(BOOL)status afterDelay:(NSTimeInterval)delay;
- (void)triggerScreenRecordStatusChanged:(BOOL)status afterDelay:(NSTimeInterval)delay;
- (void)triggerDebuggerStatus:(BOOL)status afterDelay:(NSTimeInterval)delay;
- (void)triggerVPNDetectionStatus:(BOOL)status afterDelay:(NSTimeInterval)delay;

The following method accepts a ShieldDeveloperModeStatus enum value for its first argument:

- (void)triggerDeveloperModeStatus:(ShieldDeveloperModeStatus)value afterDelay:(NSTimeInterval)delay;

The following methods only accept a delay interval argument:

- (void)triggerEmulatorDetectedAfterDelay:(NSTimeInterval)delay;
- (void)triggerScreenshotDetectedAfterDelay:(NSTimeInterval)delay;
- (void)triggerLibraryInjectionPreventedAfterDelay:(NSTimeInterval)delay;
- (void)triggerLibraryInjectionDetectedAfterDelay:(NSTimeInterval)delay;
- (void)triggerHookingFrameworksDetectedAfterDelay:(NSTimeInterval)delay;

The triggerLibraryInjectionPreventedAfterDelay method is deprecated and will be removed in a future release of App Shielding.