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

在iOS 與Android上實(shí)現(xiàn)React Native應(yīng)用的嘗試鏈接

移動(dòng)開發(fā) Android
本文將從零開始創(chuàng)建一個(gè)應(yīng)用,讓它支持通過一個(gè)如 deep-linking://articles/{ID} 這樣的 URL 打開 文章詳情 頁(yè)面,同時(shí)加載 {ID} 指定的文章,比如:deep-linking://articles/4 將打開 ID 為 4 的文章詳情頁(yè)面。

我們生活在一個(gè)萬(wàn)物兼可分享的年代,而分享的過程,幾乎最終都會(huì)分享某一個(gè)鏈接,那么,作為開發(fā)者,最常遇到的問題中應(yīng)該包括如何通過一個(gè)URL地址快速的打開App,并導(dǎo)航至特定的頁(yè)面。

在iOS 與Android上實(shí)現(xiàn)React Native應(yīng)用的嘗試鏈接

什么是深度鏈接(Deep Link)

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

其實(shí)這個(gè)技術(shù)在很久很久以前就已經(jīng)存在了,鼠標(biāo)點(diǎn)擊一下 mailto:pantao@parcmg.com 這樣的鏈接,系統(tǒng)會(huì)打開默認(rèn)的郵件軟件,然后將 pantao@parcmg.com 這個(gè)郵箱填寫至收件人輸入欄里,這就是深度鏈接。

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

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

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

實(shí)現(xiàn)深度鏈接的不同方式

有兩種方式可以實(shí)現(xiàn)深度鏈接:

  • URL scheme
  • Universal links

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

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

 

  1. react-native init DeepLinkingExample 
  2. cd DeepLinkingExample 

安裝必要的庫(kù)

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

 

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

我們將使用 react-navigation 模塊作為 App 的導(dǎo)航庫(kù)。

添加 TypeScript 相關(guān)的開發(fā)依賴:

 

  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" 

編寫我們的應(yīng)用

在項(xiàng)目根目錄下創(chuàng)建一個(gè) src 目錄,這個(gè)將是項(xiàng)目原代碼的目錄。

添加 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 

點(diǎn)擊 Info Tab 頁(yè),找到 URL Types 配置,添加一項(xiàng):

  • identifier:deep-linking
  • URL Schemes:deep-linking
  • 其它兩項(xiàng)留空

打開項(xiàng)目跟目錄下的 AppDelegate.m 文件,添加一個(gè)新的 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]; 

至此,我們已經(jīng)完成了 iOS 的配置,運(yùn)行并測(cè)試是否成功。

  1. react-native run-ios 

打開 simulator 之后,打開 Safari 瀏覽器,在地址欄中輸入:deep-linking://article/4 ,你的應(yīng)用將會(huì)自動(dòng)打開,并同時(shí)進(jìn)入到 Article 頁(yè)面。

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

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

配置 Android

要為Android應(yīng)用也創(chuàng)建 External Linking,需要?jiǎng)?chuàng)建一個(gè)新的 intent,打開 android/app/src/main/AndroidManifest.xml,然后在 MainActivity 節(jié)點(diǎn)添加一個(gè)新的 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 只需要完成上面的配置即可。

執(zhí)行:

  1. react-native run-android 

打開系統(tǒng)瀏覽器,輸入:

  1. deep-linking://article/4 

系統(tǒng)會(huì)自動(dòng)打開你的應(yīng)用,并進(jìn)入 Article 頁(yè)面

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

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

 

責(zé)任編輯:未麗燕 來源: 大胡子農(nóng)民工潘半仙
相關(guān)推薦

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-12-11 10:25:02

FlatpakLinux

2023-11-21 10:25:28

LinuxFlathubFlatpak

2023-09-04 08:32:43

web開發(fā)圖像

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

開發(fā)Flutter框架

2025-02-20 12:00:13

React前端React 19

2017-10-18 12:22:43

NativeHybirdJavaScript

2015-05-12 09:40:11

WindowsAndroidiOS
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 国产精品久久国产精品久久 | 日韩一二三区视频 | 视频一区欧美 | 在线免费av电影 | 亚洲精品一区二区网址 | 国产一区在线看 | 久久人人网| 成人免费视频网站在线看 | 欧美精品一区二区三区在线播放 | 色黄爽| 色女人天堂 | 久久亚洲国产精品日日av夜夜 | 成人做爰999| 久久国产一区二区三区 | 国产一区二区三区四区五区加勒比 | 日本午夜免费福利视频 | 99精品99| av网站免费观看 | 草草视频在线免费观看 | 国产精品毛片av一区 | 国产二区三区 | 日韩在线h | 懂色中文一区二区在线播放 | 精品1区2区| 日韩视频在线观看中文字幕 | 日韩欧美三区 | 日韩在线成人 | 久久高清国产 | 久久久日韩精品一区二区三区 | 欧美一级免费看 | 韩国精品在线 | 成人动慢| a免费视频| 成人国产免费视频 | 亚洲午夜精品一区二区三区他趣 | 久久狠狠 | 免费观看一级特黄欧美大片 | 91婷婷韩国欧美一区二区 | 亚洲欧美中文日韩在线 | 夜夜摸夜夜操 | 免费a网站 |