In this tutorial, you are going to build a small demo by adding the feature of a deep linking in a React Native application. To support deep linking and navigation in the app, react-navigation library is going to be used. There are use cases in which using an external URL a user is going to transit to a specific screen in your application. This URL can be provided on a web page, for instance. Navigating to the exact screen in the React Native app from an external URL is what the process of Deep Linking is about.

deep linking react native

In this article, we are going to build a simple deep linking mechanism in React Native, which takes a user to a specific screen of a sample app, after clicking on a deep link outside the app (e.g. Safari). The experience will look like this:

deep linking react native

Create a React Native app

To get started, please create a new React Native project by executing the following command:

react-native init rnDeepLinkDemo

To be able to support Deep linking navigation, let us add the required npm packages. Once the project directory has been generated, navigate inside the project folder from your terminal and install the following dependencies.

yarn add react-navigation react-navigation-stack react-native-gesture-handler react-native-reanimated [email protected]

The next step is to link all the libraries you just installed. I am using React Native version greater than 0.60.x. If you are using a lower version of React Native, please follow the instructions to link the libraries here.

For the iOS platform, you just have to run the following set of commands.

cd ios
pod install

For the Android platform, add the following lines to the android/app/build.gradle file under dependencies section.

implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0-alpha02'

Next, go to android/app/src/main/java/com/rndeeplinkdemo/MainActivity.java file and add the following.

// Add this with other import statements
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;

// Add this inside "class MainActivity"

@Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new ReactActivityDelegate(this, getMainComponentName()) {
@Override
protected ReactRootView createRootView() {
return new RNGestureHandlerEnabledRootView(MainActivity.this);
}
};
}

Create two mock screens

After the dependencies have installed and react-navigation configured for each platform, create two mock screen files inside the src/screens directory. First, create a file HomeScreen.js with the following code snippet. This screen is going to be the initial screen when the app loads.

import React from 'react'
import { View, Text } from 'react-native'

function HomeScreen() {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>Home Screen</Text>
    </View>
  )
}

export default HomeScreen

Next, create another file DeepLinkScreen.js. This screen is going to open when the user is transiting from a URL.

import React from 'react'
import { View, Text } from 'react-native'

function DeepLinkScreen() {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>Deep Link Screen</Text>
    </View>
  )
}

export default DeepLinkScreen

Configure Deep Linking in React Navigation

Let us create a stack navigator from both the screen files. Import the following statements inside the file src/navigation.js.

import React from 'react'
import { createAppContainer, createSwitchNavigator } from 'react-navigation'
import { createStackNavigator } from 'react-navigation-stack'

import HomeScreen from './screens/HomeScreen'
import DeepLinkScreen from './screens/DeepLinkScreen'

Next, create a stack navigator with HomeScreen as the initial screen.

const MainApp = createStackNavigator({
  Home: {
    screen: HomeScreen,
    navigationOptions: {
      headerTitle: 'Home'
    }
  },
  DeepLink: {
    screen: DeepLinkScreen,
    navigationOptions: {
      headerTitle: 'DeepLink'
    }
  }
})

To enable deep linking the app requires some identifier to recognize the path to the screen to open from the given external URL. react-navigation has path attribute that tells the router relative path to match against the URL. Re-configure both the routes as following:

const MainApp = createStackNavigator({
  Home: {
    screen: HomeScreen,
    navigationOptions: {
      headerTitle: 'Home'
    },
    path: 'Home'
  },
  DeepLink: {
    screen: DeepLinkScreen,
    navigationOptions: {
      headerTitle: 'DeepLink'
    },
    path: 'rndeeplink'
  }
})

const AppContainer = createAppContainer(MainApp)

The last piece of the puzzle here is to configure the navigation contain to extract the path from the incoming URL from an external resource. This is done by uriPrefix. Add the following code snippet at the end of the file.

export default () => {
  const prefix = 'myapp://'

  return <AppContainer uriPrefix={prefix} />
}

Do not forget to import the navigator inside App.js file, which is the main entry point of the current React Native app.

import React from 'react'
import AppContainer from './src/navigation'

function App() {
  return <AppContainer />
}

export default App

Configure URI scheme for native IOS apps

To make this work, you have to configure the native iOS app to open URLs based on the prefix myapp://. Open ios/rnDeepLinkDemo/AppDelegate.m file and add the following.

// Add the header at the top of the file:
#import <React/RCTLinkingManager.h>

// Add this above the `@end`:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url
 options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
 return [RCTLinkingManager application:app openURL:url options:options];
}

Then, open the ios/rnDeepLinkDemo.xcodeproj in the Xcode app. Select the app from the left navigation bar.

react native deep linking iOS

Open the Info tab.

Next, go to the URL Types.

Click the + button and in identifier as well as URL schemes add myapp.

Rebuild the react native binaries by running react-native run-ios.

Testing the Deep Link

The external URL is going to look like this:

myapp://rndeeplink

To test it out, there are two ways. You can either test by executing the command below in a new terminal tab.

xcrun simctl openurl booted myapp://rndeeplink

Or, open the Safari web browser in your iOS device or simulator. Type in the URL and press enter.


Leave a Reply

Your email address will not be published. Required fields are marked *

Shopping Cart