Deeplink in React Native app with React Navigation v5
Deep linking is when a link sends users directly into a specific point in the app experience, rather than an external website or app homepage.
Whats is Deeplink
- A deep link is a link that takes you to content.
- Deep linking is when a link sends users directly into a specific point in the app experience, rather than an external website or app homepage.
- Most web links are deep links.
Types of Deep Linking
- Custom URI Schemes
- iOS Universal Links
- Android App Links
URI Schemes
- Custom URI schemes were the original form of deep linking for mobile apps.
- They are like creating a “private internet” for your app, with links that look like
demo://path/to/content
. - The advantage of custom URI schemes is they are easy to set up.
- The disadvantage is a user’s device only knows about this “private internet” if the corresponding app is already installed, and there is no graceful fallback option by default.
Universal Links
- Apple introduced Universal Links in iOS 9 as a solution to the lack of graceful fallback functionality in custom URI scheme deeplinks.
- Universal Links are standard web links (
https://ankitkumar.dev
) that point to both a web page and a piece of content inside an app. - When a Universal Link is opened, iOS checks to see if any installed app on device is registered for that domain.
- If so, the app is launched immediately without ever loading the web page.
- If not, the web URL (which can be a simple redirect to the App Store) is loaded in Safari.
Android App Links
- Google built App Links as the Android equivalent to iOS Universal Links.
- They operate in a very similar way:
- a standard web link that points to both a web page and a piece of content inside an app.
- This results in a smoother user experience.
- Since custom URI schemes are still fully supported by every version of Android, App Links have seen very low adoption.
What are we building?
I am decalring deeplink urls for our application, which will open our app from anywhere in the os on Android and iOS devices.
demo://app/home/:id
- This deeplink will open home screen of the app and will passid
as param/props to home screendemo://app/profile/:id
- This deeplink will open profile screen of the app and will passid
as param/props to profile screendemo://app/notifications
- This deeplink will open notificatiosn screen of the appdemo://app/settings
- This deeplink will open notificatiosn screen of the app
After implementation of deeplink, app will behave as shown here.
Let's do some code now!
Setting up the project
I am assuming that you already have project in which deeplink need to be integrated.
If you dont have any project, I have created a small app with four screens and expalined here.
Setting up custom uri scheme for iOS in Xcode
- Open your
react-native
(deeplinkreactnavigation
) project and go toios
folder. - Open file with extension
.xcworkspace
by double clicking on it. In this projectdeeplinkreactnavigation.xcworkspace
is the file. - After opening in Xcode, follow the steps from screenshot below and add
demo
toURL Schemes
andtarget name
(deeplinkreactnavigation
) to Identifier.
Setting up custom uri scheme for Android in Android Studio
- Open your
react-native
(deeplinkreactnavigation
) project and go toandroid
folder. - Open file
build.gradle
with Android Studio. - After opening in Xcode, open
Androidmanifest.xml
and addintent-filter
as shown below.
Handling deeplink in react native
- Create a new file
linking.js
- Add
prefixes
as array ofdemo://app
and alldeeplink
urls as described above to the file as shown below
Using linking.js
in App.js
- import
linking
inApp.js
- Add it to
NavigationContainer
as shown below
We are done with the coding part
It was easy, wasn't it?
Testing deeplink
Its always easy and better to test deeplink on physical devices, so
- Install app on devcies(
Android
oriOS
orboth
) - Have the deeplink url in any other app
- Tap on deeplink url as a normal url
- Its hsould take you the our app's respective screen
If you want to test deeplink on virtual devices, then
- Install app on virtual devcies(
Android
oriOS
orboth
) - Type command
npx uri-scheme open demo://app/notifications --android
forandroid
to deeplink tonotifications
screen - Type command
npx uri-scheme open demo://app/notifications --ios
forios
to deeplink tonotifications
screen - And see the magic
Testing Video
Also, to be notified about my new articles and stories:
Subscribe to my YouTube Channel
Follow me on Medium, Github, and Twitter.
You can find me on LinkedIn as well.
I am quite active on Dev Community as well and write small topics over there.
Cheers!!