Terminate mobile app programmatically

CY Lim
3 min readApr 8, 2019

--

Why exit app programmatically?

There are a few reasons why exit app manually,

  1. Business Logic. Limited access due to copyright, licensing or other business concerns.
  2. Guided Access. Preventing normal users to exit the app by pressing home, back or power button.
  3. Without triggering Crash Logging. Properly exit the app due to business needs should never filed as crash.

How to exit app programmatically for Native app?

# iOS
exit(0);
# Android
import android.os.Process;
Process.killProcess(Process.myPid());

How to exit app for React Native?

Since the exit code is written in native code(Obj-c or Java), in order to use native code in React Native, we need to create a simple Native Modules.

For iOS (obj-c)

We have to create a header file(.h) and implementation file(.m) for the module, let said if the module it known as ExitManager .

header file extended RCTBridgeModule, which came with RCT_EXPORT_METHOD and RCT_EXPORT_MODULE which we are going to use in the implementation file.

RCT_EXPORT_MODULE export the module with current Implementation name(ExitManager) or name specified in the method argument, e.g. RCT_EXPORT_MODULE(NAME).

RCT_EXPORT_METHOD export the method with the name specified as the first argument, if we want to rename the function, we can use `RCT_REMAP_METHOD` instead.

For Android

We need to create Package and Module file for the ExitManager source code, and import the Package into MainApplication, so that the React Native app can make use of the code.

Basically, the getName() function is used to return the module name which should be similar to the module exported from iOS native code.

Annotation @ReactMethod is used for exposing the method with the Native Modules to the React Native Code.

Make sure the method and Module name is similar in both iOS and Android Native code. If it is a complicated Native Modules, you might need to remap the function name or perform certain checking in the React Native code to determine which functions needed to be running or whether both platform has the similar method.

Use Native Modules in React Native

import { NativeModules } from 'react-native'NativeModules.ExitManager.exitApp()

Why not using existing third party library?

There are a few reasons why I choose to implement the features manually instead of directly using third party solution.

  1. Simple logic. It contains a few lines of code for both platform to achieve the same behaviour.
  2. Bundle sizes and building time. Although it doesn’t affect a lot, the more smaller third-party libraries added, the more overhead for building the app. Especially for iOS with Cocoapods.
  3. Potential security risk. Although it is not happening in this case, someone might contribute malicious code into any library.
  4. Lack of maintenance. Even when there are solutions and merge requests, the repo owner might be busy of maintaining the library, which you need fork the repo and maintaining it yourself.

The purposes of this article is to learn how to exit app manually and how to code simple native modules and use it with React Native. There are a few libraries about exiting app, you can use it if you can cope with the license and bugs.

I am not against separating codes into different modules and/or library for reuse and cleaner code, in contrary, I am prefer to use third party library. With separated library, you can easily reuse the code for multiple projects if those projects have the feature.

Photo by Oskar Yildiz on Unsplash

--

--