Feature Highlight: AppSpector’s Custom Commands Tool

Igor AsharenkovApril 27, 2023

Share:

With the most recent AppSpector release, we brought a highly requested feature to our iOS and Android SDK - custom commands.

The commands monitor allows development teams to remotely run custom commands on their mobile devices via a web-based interface. Developers can preconfigure these commands to perform specific tasks or functions within the app, such as updating content, triggering events, or retrieving data.

The ability to run these custom commands remotely allows developers to quickly debug and alter their mobile apps' behavior without uploading new builds.

In this post, we'll take a closer look at how the AppSpector commands monitor works, its key features, and how it can help you improve the functionality and performance of your mobile apps. Whether you're a seasoned developer or just starting with mobile app development, read on to learn more about this exciting new feature.

Custom commands allow you to define callbacks inside your application code using AppSpector SDK and call them from our Web interface, passing a predefined set of arguments.

Custom command examples:

  • Get an internal state from application objects
  • Clear application caches
  • Generate test data for QA
  • Toggle app features
  • Access specific iOS or Android APIs

How to use custom commands

Each command is identified by a unique name and can carry a custom payload that passes from Web UI to your application code. Application callbacks can optionally return a custom response or error that transfers back to Web UI.

Example for iOS SDK (Get authentication status from internal app service)

class GetAppAuthStatus: ASCommand {
    override class var category: String { "utils" }
    override class var name: String { "show-alert" }
    override init() {}
}

// register command

AppSpector.addCommand(GetAppAuthStatus.self, withCallback: { command in
   let status = AuthService.shared().status;
   command.complete(status);
})

Example for Android SDK (Show toast for the user and send back answer to the Ultimate Question of Life, the Universe)


@Command(value = "Show message", category = "Application")
public class ShowToastCommand extends BaseCommand<Integer> {
    @Argument(isRequired = true)
    public String message;
}

AppSpector.shared().commands().register(ShowToastCommand.class, new CommandCallback<Integer, ShowToastCommand>() {
    @Override
    public void exec(@NonNull final ShowToastCommand command, @NonNull final Responder<Integer> responder) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), command.message, Toast.LENGTH_SHORT).show();
                responder.ok(42);
            }
        });
    }
});

This mechanism allows developers to define any custom behavior required for debugging or maintenance. It can be a valuable tool for debugging APIs that do not have built-in debug tools. Using custom commands, developers can remotely execute code within the app that can help diagnose and troubleshoot issues with the API, like print views hierarchy or simulate a notification.

Debugging networking code is another use case for commands. The developer can use them to temporarily modify the app's behavior to bypass the API and test other parts of the app. This can help determine if the problem is with the API response or other app features.

Adding additional logging or monitoring to the app to gather more information about the API's performance and usage can also be helpful. Now you don't need to comment on parts of the code to go the happy path and reach the problem point.

Implementation details

All commands dispatch on an internal thread. You should only interact with UI from command callback by explicitly dispatching it on the main thread.

Command's internal implementation may contain references to internal SDK objects. You should not retain a command object received in the callback for a long time. It may cause memory leaks.

AppSpector.addCommand(MyCommand.self, withCallback: { command in
   // don't do this!
   executed_commands.push(command);
})

Summary

The custom commands monitor is an important feature that covers custom developer needs and missing AppSpector SDK features. Installation is easy for both iOS and Android SDKs.