成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

在iOS 與Android上實現React Native應用的嘗試鏈接

移動開發 Android
本文將從零開始創建一個應用,讓它支持通過一個如 deep-linking://articles/{ID} 這樣的 URL 打開 文章詳情 頁面,同時加載 {ID} 指定的文章,比如:deep-linking://articles/4 將打開 ID 為 4 的文章詳情頁面。

我們生活在一個萬物兼可分享的年代,而分享的過程,幾乎最終都會分享某一個鏈接,那么,作為開發者,最常遇到的問題中應該包括如何通過一個URL地址快速的打開App,并導航至特定的頁面。

在iOS 與Android上實現React Native應用的嘗試鏈接

什么是深度鏈接(Deep Link)

深度鏈接是一項可以讓一個App通過一個URL地址打開,之后導航至特定頁面或者資源,或者展示特定UI的技術,Deep 的意思是指被打開的頁面或者資源并不是App的首頁,最常使用到的地方包括但遠遠不限于 Push Notification、郵件、網頁鏈接等。

其實這個技術在很久很久以前就已經存在了,鼠標點擊一下 mailto:pantao@parcmg.com 這樣的鏈接,系統會打開默認的郵件軟件,然后將 pantao@parcmg.com 這個郵箱填寫至收件人輸入欄里,這就是深度鏈接。

本文將從零開始創建一個應用,讓它支持通過一個如 deep-linking://articles/{ID} 這樣的 URL 打開 文章詳情 頁面,同時加載 {ID} 指定的文章,比如:deep-linking://articles/4 將打開 ID 為 4 的文章詳情頁面。

深度鏈接解決了什么問題?

網頁鏈接是無法打開原生應用的,如果一個用戶訪問你的網頁中的某一個資源,他的手機上面也已經安裝了你的應用,那么,我們要如何讓系統自動的打開應用,然后在應用中展示用戶所訪問的那一個頁面中的資源?這就是深度鏈接需要解決的問題。

實現深度鏈接的不同方式

有兩種方式可以實現深度鏈接:

  • URL scheme
  • Universal links

前端是最常見的方式,后者是 iOS 新提供的方式,可以一個普通的網頁地址鏈接至App的特定資源。

本文將創建一個名為 DeepLinkingExample 的應用,使得用戶可以通過打開 deep-linking://home 以及 deep-linking://articles/4 分別打開 App 的首頁以及 App 中 ID 為 4 的文章詳情頁面。

 

  1. react-native init DeepLinkingExample 
  2. cd DeepLinkingExample 

安裝必要的庫

緊跟 TypeScript 大潮流,我們的 App 寫將使用 TypeScript 開發。

 

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

我們將使用 react-navigation 模塊作為 App 的導航庫。

添加 TypeScript 相關的開發依賴:

 

  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 

添加 tsconfig.json:

 

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

添加 tslint.json 文件

 

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

添加 .prettierrc 文件:

 

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

編寫我們的應用

在項目根目錄下創建一個 src 目錄,這個將是項目原代碼的目錄。

添加 src/App.tsx 文件

  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. const prefix = 'deep-linking://' 
  20.  
  21. const App = createAppContainer(AppNavigator) 
  22.  
  23. const MainApp = () => <App uriPrefix={prefix} /> 
  24.  
  25. export default MainApp 

添加 src/screens/Home.tsx 文件

  1. import React from 'react'

添加 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. AboutScreen.navigationOptions = { 
  19.   title: 'About'
  20.  
  21. export default AboutScreen 
  22.  
  23. const styles = StyleSheet.create({ 
  24.   container: {}, 
  25.   title: {}, 
  26. }) 

添加 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. const ArticleScreen: NavigationScreenComponent<NavigationParams> = ({ navigation }) => { 
  11.   const { params } = navigation.state 
  12.  
  13.   return ( 
  14.     <View style={styles.container}> 
  15.       <Text style={styles.title}>Article {params ? params.id : 'No ID'}</Text> 
  16.     </View
  17.   ) 
  18.  
  19. ArticleScreen.navigationOptions = { 
  20.   title: 'Article'
  21.  
  22. export default ArticleScreen 
  23.  
  24. const styles = StyleSheet.create({ 
  25.   container: {}, 
  26.   title: {}, 
  27. }) 

配置 iOS

打開 ios/DeepLinkingExample.xcodeproj:

  1. open ios/DeepLinkingExample.xcodeproj 

點擊 Info Tab 頁,找到 URL Types 配置,添加一項:

  • identifier:deep-linking
  • URL Schemes:deep-linking
  • 其它兩項留空

打開項目跟目錄下的 AppDelegate.m 文件,添加一個新的 import:

  1. #import "React/RCTLinkingManager.h" 

然后在 @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]; 

至此,我們已經完成了 iOS 的配置,運行并測試是否成功。

  1. react-native run-ios 

打開 simulator 之后,打開 Safari 瀏覽器,在地址欄中輸入:deep-linking://article/4 ,你的應用將會自動打開,并同時進入到 Article 頁面。

同樣的,你還可以在命令行工具中執行以下命令:

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

配置 Android

要為Android應用也創建 External Linking,需要創建一個新的 intent,打開 android/app/src/main/AndroidManifest.xml,然后在 MainActivity 節點添加一個新的 intent-filter:

 

  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 只需要完成上面的配置即可。

執行:

  1. react-native run-android 

打開系統瀏覽器,輸入:

  1. deep-linking://article/4 

系統會自動打開你的應用,并進入 Article 頁面

也可以在命令行工具中使用以下命令打開:

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

 

責任編輯:未麗燕 來源: 大胡子農民工潘半仙
相關推薦

2015-03-30 12:13:23

React NativiOS

2016-08-12 08:49:46

React NativFacebookNative

2018-01-02 16:08:00

AndroidiOSReact Nativ

2015-09-22 09:50:36

FacebookAndroid

2016-11-23 16:48:20

react-nativandroidjavascript

2016-08-15 13:34:37

React NativiOSjs入口

2011-02-25 15:49:09

NecessitasQtAndroid

2023-11-21 10:25:28

LinuxFlathubFlatpak

2023-12-11 10:25:02

FlatpakLinux

2023-09-04 08:32:43

web開發圖像

2011-11-02 13:56:13

2015-10-10 16:02:36

React NativAndroid

2017-05-03 15:00:59

PC樹莓派PIXEL OS

2017-08-15 19:20:51

AndroidHttpServer

2023-03-07 16:12:32

2012-03-08 22:29:41

Android

2019-08-29 09:00:55

開發Flutter框架

2025-02-20 12:00:13

React前端React 19

2017-10-18 12:22:43

NativeHybirdJavaScript

2015-05-12 09:40:11

WindowsAndroidiOS
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产真实乱对白精彩久久小说 | 免费人成激情视频在线观看冫 | 亚洲国产高清高潮精品美女 | 正在播放一区二区 | 欧美大片一区 | 欧美成人精品在线 | 亚洲精品成人在线 | 桃色五月 | 日本精品久久 | 亚洲黄色一级毛片 | 国产精品美女一区二区 | 国产美女精品 | 先锋资源亚洲 | 亚洲精品一区二三区不卡 | 国产精品久久久久久久久久三级 | 国产观看 | 不卡视频在线 | 亚洲精品国产偷自在线观看 | 欧美日韩视频网站 | 亚洲综合中文字幕在线观看 | 亚洲精品视频在线播放 | 91亚洲国产成人久久精品网站 | 欧美日韩精品在线免费观看 | 亚洲三级在线 | 亚洲国产情侣 | 国产福利视频导航 | 国产片侵犯亲女视频播放 | 日韩在线欧美 | 亚洲日本欧美日韩高观看 | 羞羞视频免费观看 | www久久av| 91久久久久久久久久久久久 | 国产91一区 | 中文字幕精 | 午夜国产在线 | 99精品国自产在线观看 | 成年人国产在线观看 | 操亚洲| 国产在线精品区 | 国产精品福利视频 | 亚洲一区在线播放 |