Try to implement React Native application on iOS and Android

Try to implement React Native application on iOS and Android

We live in an age where everything can be shared, and the process of sharing almost always ends up sharing a link. So, as a developer, the most common problems should include how to quickly open an App through a URL address and navigate to a specific page.

What is Deep Link?

Deep linking is a technology that allows an App to be opened through a URL address and then navigate to a specific page or resource, or display a specific UI. Deep means that the page or resource opened is not the homepage of the App. The most commonly used places include but are far from limited to Push Notification, emails, web links, etc.

In fact, this technology has existed for a long time. When you click a link like mailto:[email protected], the system will open the default email software, and then fill in the email address [email protected] in the recipient input field. This is a deep link.

This article will create an application from scratch that supports opening an article detail page through a URL such as deep-linking://articles/{ID} and loading the article specified by {ID}. For example, deep-linking://articles/4 will open the article detail page with ID 4.

What problem does deep linking solve?

Web links cannot open native applications. If a user visits a resource on your web page and your application is already installed on his phone, how can we let the system automatically open the application and then display the resources on the page visited by the user in the application? This is the problem that deep links need to solve.

Different ways to implement deep linking

There are two ways to implement deep linking:

  • URL scheme
  • Universal links

The front end is the most common method, and the latter is a new method provided by iOS, which can link to specific resources of the App with a normal web address.

This article will create an app named DeepLinkingExample, which allows users to open the home page of the App and the details page of the article with ID 4 in the App by opening deep-linking://home and deep-linking://articles/4 respectively.

  1. react-native init DeepLinkingExample
  2. cd DeepLinkingExample

Install Necessary Libraries

Following the TypeScript trend, our App will be developed using TypeScript.

  1. yarn add react-navigation react-native-gesture-handler
  2. react-native link react-native-gesture-handler

We will use the react-navigation module as the navigation library for the App.

Add TypeScript related development dependencies:

  1. yarn add typescript tslint tslint-react tslint-config-airbnb tslint-config-prettier ts-jest react-native-typescript-transformer -D
  2. yarn add @types/jest @types/node @types/react @types/react-native @types/react-navigation @types/react-test-renderer

Add tsconfig.json:

  1. {
  2. "compilerOptions" : {
  3. "target" : "es2017" , /* Specify ECMAScript target version: 'ES3' ( default ), 'ES5' , 'ES2015' , 'ES2016' , 'ES2017' , or   'ESNEXT' . */
  4. "module" : "es2015" , /* Specify module code generation: 'none' , 'commonjs' , 'amd' , 'system' , 'umd' , 'es2015' , or   'ESNext' . */
  5. "lib" : [ /* Specify library files to be included in the compilation: */
  6. "es2017" ,
  7. "dom"  
  8. ],
  9. "resolveJsonModule" : true ,
  10. "allowJs" : false , /* Allow javascript files to be compiled. */
  11. "skipLibCheck" : true , /* Skip type checking of   all declaration files. */
  12. "jsx" : "react-native" , /* Specify JSX code generation: 'preserve' , 'react-native' , or   'react' . */
  13. "declaration" : true , /* Generates corresponding '.d.ts' file. */
  14. "sourceMap" : true , /* Generates corresponding '.map' file. */
  15. "outDir" : "./lib" , /* Redirect output structure to the directory. */
  16. "removeComments" : true , /* Do not emit comments to   output . */
  17. "noEmit" : true , /* Do not emit outputs. */
  18.  
  19. /* Strict Type-Checking Options */
  20. "strict" : true , /* Enable all strict type-checking options. */
  21. "noImplicitAny" : true , /* Raise error on expressions and declarations with an implied 'any' type. */
  22. "strictNullChecks" : true , /* Enable strict null checks. */
  23. "strictFunctionTypes" : true , /* Enable strict checking of   function types. */
  24. "noImplicitThis" : true , /* Raise error on   'this' expressions with an implied 'any' type. */
  25. "alwaysStrict" : true , /* Parse in strict mode and emit "use strict"   for each source file. */
  26.  
  27. /* Additional Checks */
  28. "noUnusedLocals" : true , /* Report errors on unused locals. */
  29. "noUnusedParameters" : true , /* Report errors on unused parameters. */
  30. "noImplicitReturns" : true , /* Report error when   not   all code paths in   Function   return a value. */
  31. "noFallthroughCasesInSwitch" : true , /* Report errors for fallthrough cases in switch statement. */
  32.  
  33. /* Module Resolution Options */
  34. "moduleResolution" : "node" , /* Specify module resolution strategy: 'node' (Node.js) or   'classic' (TypeScript pre-1.6). */
  35. "baseUrl" : "./" , /* Base directory to resolve non- absolute module names. */
  36. "paths" : { /* A series of entries which re-map imports to lookup locations relative   to the 'baseUrl' . */
  37. "*" : [
  38. "*.android" ,
  39. "*.ios" ,
  40. "*.native" ,
  41. "*.web" ,
  42. "*"  
  43. ]
  44. },
  45. "typeRoots" : [ /* List of folders to include type definitions from . */
  46. "@types" ,
  47. "../../@types"  
  48. ],
  49. // "types" : [], /* Type declaration files to be included in compilation. */
  50. "allowSyntheticDefaultImports" : true , /* Allow default imports from modules with   no   default export. This does not affect code emit, just typechecking. */
  51. // "preserveSymlinks" : true , /* Do not resolve the real path of symlinks. */
  52.  
  53. /* Experimental Options */
  54. "experimentalDecorators" : true , /* Enables experimental support for ES7 decorators. */
  55. "emitDecoratorMetadata" : true /* Enables experimental support for emitting type metadata for decorators. */
  56. },
  57. "exclude" : [
  58. "node_modules" ,
  59. "web"  
  60. ]
  61. }

Add tslint.json file

  1. {
  2. "defaultSeverity" : "warning" ,
  3. "extends" : [
  4. "tslint:recommended" ,
  5. "tslint-react" ,
  6. "tslint-config-airbnb" ,
  7. "tslint-config-prettier"  
  8. ],
  9. "jsRules" : {},
  10. "rules" : {
  11. "curly" : false ,
  12. "function-name" : false ,
  13. "import-name" : false ,
  14. "interface-name" : false ,
  15. "jsx-boolean-value" : false ,
  16. "jsx-no-multiline-js" : false ,
  17. "member-access" : false ,
  18. "no-console" : [ true , "debug" , "dir" , "log" , "trace" , "warn" ],
  19. "no-empty-interface" : false ,
  20. "object-literal-sort-keys" : false ,
  21. "object-shorthand-properties-first" : false ,
  22. "semicolon" : false ,
  23. "strict-boolean-expressions" : false ,
  24. "ter-arrow-parens" : false ,
  25. "ter-indent" : false ,
  26. "variable-name" : [
  27. true ,
  28. "allow-leading-underscore" ,
  29. "allow-pascal-case" ,
  30. "ban-keywords" ,
  31. "check-format"  
  32. ],
  33. "quotemark" : false  
  34. },
  35. "rulesDirectory" : []
  36. }

Add .prettierrc file:

  1. {
  2. "parser" : "typescript" ,
  3. "printWidth" : 100,
  4. "semi" : false ,
  5. "singleQuote" : true ,
  6. "trailingComma" : "all"  
  7. }

Writing our application

Create a src directory in the project root directory, which will be the directory for the project source code.

Add src/App.tsx file

  1. import React from   'react'  
  2.  
  3. import { createAppContainer, createStackNavigator } from   'react-navigation'  
  4.  
  5. import About from   './screens/About'  
  6. import Article from   './screens/Article'  
  7. import Home from   './screens/Home'  
  8.  
  9. const AppNavigator = createStackNavigator(
  10. {
  11. Home: { screen: Home },
  12. About: { screen: About, path: 'about' },
  13. Article: { screen: Article, path: 'article/:id' },
  14. },
  15. {
  16. initialRouteName: 'Home' ,
  17. },
  18. )
  19.  
  20. const prefix = 'deep-linking://'  
  21.  
  22. const App = createAppContainer(AppNavigator)
  23.  
  24. const MainApp = () => <App uriPrefix={prefix} />
  25.  
  26. export default MainApp

Add src/screens/Home.tsx file

  1. import React from   'react' ;

Add src/screens/About.tsx

  1. import React from   'react'  
  2.  
  3. import { StyleSheet, Text, View } from   'react-native'  
  4.  
  5. import { NavigationScreenComponent } from   'react-navigation'  
  6.  
  7. interface IProps {}
  8.  
  9. interface IState {}
  10.  
  11. const AboutScreen: NavigationScreenComponent<IProps, IState> = props => {
  12. return (
  13. < View style={styles.container}>
  14. <Text style={styles.title}>About Page</Text>
  15. </ View >
  16. )
  17. }
  18.  
  19. AboutScreen.navigationOptions = {
  20. title: 'About' ,
  21. }
  22.  
  23. export default AboutScreen
  24.  
  25. const styles = StyleSheet. create ({
  26. container: {},
  27. title: {},
  28. })

Add src/screens/Article.tsx

  1. import React from   'react'  
  2.  
  3. import { StyleSheet, Text, View } from   'react-native'  
  4.  
  5. import { NavigationScreenComponent } from   'react-navigation'  
  6.  
  7. interface NavigationParams {
  8. id: string
  9. }
  10.  
  11. const ArticleScreen: NavigationScreenComponent<NavigationParams> = ({ navigation }) => {
  12. const { params } = navigation.state
  13.  
  14. return (
  15. < View style={styles.container}>
  16. <Text style={styles.title}>Article {params ? params.id : 'No ID' }</Text>
  17. </ View >
  18. )
  19. }
  20.  
  21. ArticleScreen.navigationOptions = {
  22. title: 'Article' ,
  23. }
  24.  
  25. export default ArticleScreen
  26.  
  27. const styles = StyleSheet. create ({
  28. container: {},
  29. title: {},
  30. })

Configuring iOS

Open ios/DeepLinkingExample.xcodeproj:

  1. open ios/DeepLinkingExample.xcodeproj

Click the Info Tab page, find the URL Types configuration, and add an item:

  • identifier:deep-linking
  • URL Schemes: deep-linking
  • Leave the other two items blank

Open the AppDelegate.m file in the project's root directory and add a new import:

  1. #import "React/RCTLinkingManager.h"  

Then add the following code before @end:

  1. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
  2. return [RCTLinkingManager application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
  3. }

So far, we have completed the configuration of iOS, running and testing whether it is successful.

  1. react-native run-ios

After opening the simulator, open the Safari browser and enter deep-linking://article/4 in the address bar. Your app will automatically open and go to the Article page at the same time.

Similarly, you can also execute the following command in the command line tool:

  1. xcrun simctl openurl booted deep-linking://article/4

Configuring Android

To create External Linking for Android applications, you need to create a new intent, open android/app/src/main/AndroidManifest.xml, and then add a new intent-filter in the MainActivity node:

  1. <application ...>
  2. <activity android: name = ".MainActivity" ...>
  3. ...
  4. <intent-filter>
  5. < action android: name = "android.intent.action.VIEW" />
  6. <category android: name = "android.intent.category.DEFAULT" />
  7. <category android: name = "android.intent.category.BROWSABLE" />
  8. <data android:scheme= "deep-linking" />
  9. </intent-filter>
  10. ...
  11. </activity>
  12. </application>

Android only needs to complete the above configuration.

implement:

  1. react-native run-android

Open the system browser and enter:

  1. deep-linking://article/4

The system will automatically open your app and enter the Article page

You can also use the following command to open it in the command line tool:

  1. adb shell am start -W -a android.intent. action . VIEW -d "deep-linking://article/3" com.deeplinkingexample;

<<:  Ten tips to speed up Android app building

>>:  The battle of billions of pixels: What are the mobile phone manufacturers competing for?

Recommend

How to control costs while improving conversion effects through advertising?

For those who are new to the industry, it may be ...

@If you often watch short videos, your brain may be affected!

To reduce the time spent on short videos and get ...

How do luxury brands use UGC marketing? Key analysis of these 3 issues

Many luxury brands are reluctant to use user-gene...

Jack Ma: Fortunately, I started my business 20 years ago

[[152236]] The annual Alibaba Cloud Conference at...

Quantum interference makes counting stars easier

The exploration of the deep night sky has been wi...

Believe it and fall for it. How many of these useless apps have you used?

With the popularity of smartphones, more and more...

"Crayon Shin-chan" Movie Edition 28 Collection (1993-2020) HD Japanese Subtitles

Contains 28 Crayon Shin-chan theatrical animations...

Soil is alive! These bugs help plants grow

Produced by: Science Popularization China Author:...

Analysis of competitive products of Jianying APP

“ Competitive product analysis is one of the esse...

The terrifying bird inherited the "legacy" of the dinosaurs!

In today's South America, there lives a stran...

Xiaohongshu KOL promotion: the secret of note flow limitation!

After writing "Little Red Book KOL Promotion...