Capture Digital Signature in React Native App for Android and iOS

Capture Digital Signature in React Native

In this post, we will see How to Capture Digital Signature in React Native App. We all know digitalization is everywhere and everyone is using digital ways to store data as it becomes very fast and easy to access the data. Taking a digital signature is also a part of this digitalization.

If I talk about the general example where we can see the use of digital signature then the logistic delivery industry is the best fit for that as they take the digital signature from the user on delivery to assure the completion of delivery. This was just an example you can implement a digital signature feature for other uses also.

Taking a digital signature in React Native is super easy with react-native-signature-capture library as it provides a SignatureCapture component with features like save or reset signature. This is how you can use the SignatureCapture component.

SignatureCapture Component

<SignatureCapture
  style={styles.signature}
  ref={sign}
  onSaveEvent={_onSaveEvent}
  onDragEvent={_onDragEvent}
  showNativeButtons={false}
  showTitleLabel={false}
  viewMode={'portrait'}
/>

This component provides the following useful props, methods, and callbacks:

Props

showBorder: If this props is made to false, it will hide the dashed border (the border is shown on iOS only).

showNativeButtons: If this props is made to true, it will display the native buttons “Save” and “Reset”.

showTitleLabel: If this props is made to true, it will display the “x_ _ _ _ _ _ _ _ _ _ _” placeholder indicating where to sign.

viewMode: “portrait” or “landscape” change the screen orientation.

maxSize: sets the max size of the image maintains aspect ratio, default is 500

Methods

saveImage(): when called it will save the image and returns the base 64 encoded string on onSaveEvent() callback

resetImage(): when called it will clear the image on the canvas

Callback Props

onSaveEvent: Triggered when saveImage() is called, which returns Base64 Encoded String and image file path.

onDragEvent: Triggered when a user marks his signature on the canvas. This will not be called when the user does not perform any action on the canvas.

Now let’s get started with the example and see how to capture the signature in React Native.

In this example, we are going to make a screen with lots of space to take the signature. We are going to make 2 custom buttons (Save and Reset) instead of native buttons. On the click of Save, we are printing the signature in base64 format on the console and generate an alert to confirm everything is good and the signature was captured. With the click of the Reset button, we will clear the canvas to sign again.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.

If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:

npm uninstall -g react-native-cli @react-native-community/cli

Run the following commands to create a new React Native project

npx react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

npx react-native init ProjectName --version X.XX.X

Note If the above command is failing, you may have old version of react-native or react-native-cli installed globally on your pc. Try uninstalling the cli and run the cli using npx.

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use SignatureCapture component we have to install react-native-signature-capture dependency. To install the dependency open the terminal and jump into your project

cd ProjectName

Now install the dependency

npm install react-native-signature-capture --save

CocoaPods Installation

Please use the following command to install CocoaPods

npx pod-install

Code to Capture Signature in React Native

Now Open App.js in any code editor and replace the code with the following code

App.js

// Capture Digital Signature in React Native App for Android and iOS
// https://aboutreact.com/react-native-capture-signature/

// import React in our code
import React, {createRef} from 'react';

// import all the components we are going to use
import {
  SafeAreaView,
  StyleSheet,
  View,
  Text,
  TouchableHighlight,
} from 'react-native';

import SignatureCapture from 'react-native-signature-capture';

const App = () => {
  const sign = createRef();

  const saveSign = () => {
    sign.current.saveImage();
  };

  const resetSign = () => {
    sign.current.resetImage();
  };

  const _onSaveEvent = (result) => {
    //result.encoded - for the base64 encoded png
    //result.pathName - for the file path name
    alert('Signature Captured Successfully');
    console.log(result.encoded);
  };

  const _onDragEvent = () => {
    // This callback will be called when the user enters signature
    console.log('dragged');
  };

  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.container}>
        <Text style={styles.titleStyle}>
          Capture Digital Signature in React Native App
        </Text>
        <SignatureCapture
          style={styles.signature}
          ref={sign}
          onSaveEvent={_onSaveEvent}
          onDragEvent={_onDragEvent}
          showNativeButtons={false}
          showTitleLabel={false}
          viewMode={'portrait'}
        />
        <View style={{flexDirection: 'row'}}>
          <TouchableHighlight
            style={styles.buttonStyle}
            onPress={() => {
              saveSign();
            }}>
            <Text>Save</Text>
          </TouchableHighlight>
          <TouchableHighlight
            style={styles.buttonStyle}
            onPress={() => {
              resetSign();
            }}>
            <Text>Reset</Text>
          </TouchableHighlight>
        </View>
      </View>
    </SafeAreaView>
  );
};
export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'white',
  },
  titleStyle: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  signature: {
    flex: 1,
    borderColor: '#000033',
    borderWidth: 1,
  },
  buttonStyle: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    height: 50,
    backgroundColor: '#eeeeee',
    margin: 10,
  },
});

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

1. Start Metro Bundler

First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:

npx react-native start

Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.

2. Start React Native Application

To run the project on an Android Virtual Device or on real debugging device:

npx react-native run-android

or on the iOS Simulator by running (macOS only)

npx react-native run-ios

Output Screenshots

      

This is how you can capture the signature in the React Native app for Android and iOS. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

10 thoughts on “Capture Digital Signature in React Native App for Android and iOS”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.