0% found this document useful (0 votes)
37 views

Deep Linking Your React Native App

Deep linking your react native app

Uploaded by

Cristhian Cruz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
37 views

Deep Linking Your React Native App

Deep linking your react native app

Uploaded by

Cristhian Cruz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 16
suncas Dea Leng Your Rest Nate op by Gna aa Meson i jet started Gilshaan Jabbar = 29Followers About ) Sionin (cetstarsa) @O “This is your last free member-only story this month. Sign up for Medium and get an extra one Deep Linking Your React Native App In this post, Pll quickly walk through how to add deep linking to a React Native app for both iOS and Android using React Navigation. HOW TO HANDLE oa Ling Your eat Nate op by nan aoa Mesum REACT NATIVE, APP Deep Linking is a technique in which a given URL or resource is used to open a specific page or screen on mobile. So, instead of just launching the app on mobile, a deep link can lead a user to a specific page within the app, providing a better experience. This specific page or screen may reside under aseries of hierarchical pages, hence the term “deep” in deep linking. There are many cases where providing a url to transition a user to a specific point in your app on load makes a lot of sense. For example, say you have a person's profile, and you want to provide a link somewhere on the web or in another app, so that when the user clicks on a link, we not only open the app but also navigate to the exact point in the app that we want to go. This is what deep linking is, and this is what we will be walking through in this post. pugnan necimcarisep ekg ourencaaiveapp Stee ane seas oer er Re ae py Sta a den Linking will provide for us an API that will allow us to listen for an incoming linked url, and then we can handle the ur! like conponentDsdMount ()[ Linking. addBventiistener(turl', this.handleOpenURL) ; ) componentwillunnount (){ Linking. renoveEventListener(‘url', this-handledpenURL) ; ) handleopenvRL (event) { console. log (event uri}; const route ~ e,url.zeplace(/.#2:\/\ide 1) // do something with the url, in our case navigate (route) , When a user in an external app or website clicks on one of our links, we will open in our new app and navigate to the intended url: peopleapp: //people/0 Peopleapp: //psopie/1 peopleapp: //psople/2 teste pugnan necimcarisep ekg ourencaaiveapp Stee ae seas ep nn Yaa Nate hp an asin That will navigate to the people route, and then show a person based on the Let's get started by creating a new React Native app and adding react- navigation native init Peoplenep Once installed, cd into the new directory and install react-navigation using pm or yarn npn i vs ct-navigation or pugnan necimcarisep ekg ourencaaiveapp Stee ane succes oop Ling Your Nave Ap by nan aba Mtn Now that we have our app and some navigation, let’s create a router and two views: a home view and the people view. Now, let’s go into incex.ios.js / index.andvoid.js and import the router will will create next. 02.52 or index. android Registry } from ‘reac import | Apch import Router AppReg Router); “Peopleapp', Now, we'll go ahead and create the router. In router.j // router. js Inport React import AppReg from "react; pugnan necimcarisep ekg ourencaaiveapp Stee oop Ling Your Nave Ap by nan aba Mtn vrext, } from! ‘react-native'; import { StackNavigator } from 'react~navigation'; import Hons from *./hone'; Anport People from './people' const Router = StackWavigator(( Home: ( 2 Home People: 2 People }, De export default Router: Then, we'll create the Home component. In rome.5s import React from 'react"; import { Platform, Text, Linking } from ‘react-native';class Hone extends React.Component. | static navigationdptions = ( // A tle: "Home, he conponentDidount () ( // B if (Platform.0s === "android') { Linking. get InitialURI () .then (url this-navigate (ur); De b else { Linking.addtventhistener("url', this.handleopenvRL) ; > ' pignan nest caridep ekg oursencaaiv spp SScHee6 succes oop Ling Your Nave Ap by nan aba Mtn componentwitlUnmount () ( // Linking. renoveRventListener ("url', this-hand!eQpenURi) : ' handleopenURL = (event) => { // D this.navigate(event.url) , navigate = (url) => ( // B const { navigate ) = this.props-navigation; const route = url.zeplace(/.*?:\/\//d, ')7 const id ~ route.mateh (/4/(C°\/14)\/28/) [11 const routeName ~ route.split('/") [0]; Af (routeNane “== "people') ( navigate ("People", { id, name: "chris? }} bh render () { return 7 n export default Home; ‘A. We declare atitle using static aavigationoptions for react-navigation to show a title when we are on this route. B. If we are on android, we immediately call the navigate method passing inthe ut . Ifwe are on iOS, We add an event listener to call nanateovent1 pugnan necimcarisep ekg ourencaaiveapp Stee me oop Ling Your Nave Ap by nan aba Mtn when an incoming link is detected. C.We delete the Linking listener on conponentwiiiveno. D. When handleOpenURL is called, we pass the event url to the navigate method. E, We first parse the url to get the id and route name. We then check to see ifthe route name is equal to peopte , and if so we navigate to the People component, passing the id as a prop. Now we can go to pecpie.is and create the component there that will be showing a person based on a route id. In peopie.is import React from 'react'; import { Text, In: people = { // A , View, Stylesheet } from ‘rea: ative! ;const "hetps //vignettel wikia nocookie .net/en. futurana/ images Leela.png/revision/lavest ?cb~20150218013044", ela, 4/4 /Tuxang pugnan necimcarisep ekg ourencaaiveapp Stee se succes oop Ling Your Nave Ap by nan aba Mtn Le rane: "Bender", thetps: //vignet: png/revision/latest7cl » ‘sikia nocookie.net/en.futurana/images/4/43/Bender 0150206072725", 2 i amet "Bay", image: 'hetps://i.yting.con/vi/4sCtTq7K3yi/hadefault. ps" b a name: "Fry', image: "hetp: //www .supergrove..com/wp-content /uploads/2017/03/fry- futurana-22-which-robot-from-quot futuramaquet~are-you.jp3" , } clase People extends React.Component ( static navigationdptions = [ tle: 'People', Me render () ( const | id ) = this.props.navigation.state.params; // B Af (Ipeopielial) Sorry, no data exists for this userreturn ( // © (people [id] .nane} pignan nectmcaridep kn oursencaiveapp SSH ne oop Ling Your Nave Ap by nan aba Mtn Jeonst styles = StyleSheet.create( A. We create a hardcoded object with some information about a few people, including only their name and image with a corresponding id key. B. We destructure the sa prop that we will be receiving. If no user exists for this id, we display a message. C. We use the ia to reference the correct person, displaying their image and name. Ok, now that we have all of our basic code set up, we now need to configure both iOS and Android to enable deep linking. Configuring iOS pugnan necimcarisep ekg ourencaaiveapp Stee sei succes oop Ling Your Nave Ap by nan aba Mtn Step 1. Add URL type to inso.plist 1. Open inso.piist and at the top of the file, create a new property called 2. Expand item 0 (zero) and choose URL Schemes. 3. Give item 0 the name you would like your app to be linkable as. In our case, Ichose pe as the name. pignan nectmcarisep ekg oursencaaiveappSScHee6 succes oop Ling Your Nave Ap by nan aba Mtn ----> peopleapp will be what we use to identify the app. Once this identifier is set up, we will be able to reference it outside of the application in web browsers omeotherinfo/. < and other apps, like $0: peopieapp://soneur Step 2. Add the following code to apr Below last existing import add this import: janager b> fimport *)opt cat ionopentRLoptionskt nkingNanager application:app openUR: options:options|; ) .egate.m should look something like this linked Github Now your final »; gist. pignan nectmcaridep kn ourene avg Stee seas ep nn Yaa Nate hp an asin Now we should be able to open up safari or chrome and pass in the following url: And the app should redirect on load to the correct route, showing the correct person's info! punan nectmcaridep ekg ourenc avg SScHee6 sae succes Dea Leng Your React Nate op by lana aba Meson Configuring Android First, we need to open our Manifest file and add the app name we will want to be referencing, in our case peopieape In enaroid/app/sre/nain Open Androscttans test xn and add the following intent filter below the last intent filter already listed, within the tag: wtent-filter android:label="filter_rea: ‘tion android:name="android. intent a tegory android:nane: intent tegory android:nane: intent android: schene: Here is an idea of how your androidanitext.xni should now look. pugnan necimcarisep ekg ourencaaiveapp Stee ie seas ep Un YR Nate hp an ba Msn The tag specifies the URL scheme which resolves to the activity in which the intent filter has been added. In this example the intent filter accepts URIs that begin with peopieapp://pcopie . To learn more about this configuration, check out this documentation. That is all we need as far as configuration goes. To test this out, open Android Studio, Open Run -> Edit Configurations and change the launch options to URL, passing in the following url: peopieapp: //peoe pignan nectmcaridep ekg oursencaiveappSScHee6 si8 succes oop Ling Your Nave Ap by nan aba Mtn Now when we run the app, we should see Bender! ‘The final code for this example can be found here, ReactNatve React Nate Developers React Programming React pugnan necimcarisep ekg ourencaaiveapp Stee see

You might also like