日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
在iOS與Android上實(shí)現(xiàn)ReactNative應(yīng)用的嘗試鏈接

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

成都創(chuàng)新互聯(lián)公司專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、阿拉山口網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁面制作、商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為阿拉山口等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

什么是深度鏈接(Deep Link)

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

其實(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 打開 文章詳情 頁面,同時(shí)加載 {ID} 指定的文章,比如:deep-linking://articles/4 將打開 ID 為 4 的文章詳情頁面。

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

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

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

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

  • URL scheme
  • Universal links

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

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

 
 
 
 
  1. react-native init DeepLinkingExample 
  2. cd DeepLinkingExample 

安裝必要的庫

緊跟 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)航庫。

添加 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 = () =>  
  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 = props => { 
  12.   return ( 
  13.      
  14.       About Page 
  15.      
  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 = ({ navigation }) => { 
  11.   const { params } = navigation.state 
  12.  
  13.   return ( 
  14.      
  15.       Article {params ? params.id : 'No ID'} 
  16.      
  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 頁,找到 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 頁面。

同樣的,你還可以在命令行工具中執(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.  
  2.    
  3.     ... 
  4.      
  5.        
  6.        
  7.        
  8.        
  9.      
  10.     ... 
  11.    
  12.  

Android 只需要完成上面的配置即可。

執(zhí)行:

 
 
 
 
  1. react-native run-android 

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

 
 
 
 
  1. deep-linking://article/4 

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

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

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

分享題目:在iOS與Android上實(shí)現(xiàn)ReactNative應(yīng)用的嘗試鏈接
URL鏈接:http://www.5511xx.com/article/dhejcis.html