Skip to content

Commit 1fe1ea3

Browse files
WoLewickiosdnk
authored andcommitted
add 'manually handling deep linking' guide (#607)
co-authored-by: dewolff@github.com
1 parent c79778c commit 1fe1ea3

File tree

3 files changed

+108
-27
lines changed

3 files changed

+108
-27
lines changed

website/versioned_docs/version-2.x/deep-linking.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ You need to specify a scheme for your app. You can register for a scheme in your
4444

4545
### URI Prefix
4646

47-
Next, let's configure our navigation container to extract the path from the app's incoming URI.
47+
Next, let's configure our navigation container to extract the path from the app's incoming URI.
4848

4949
```js
5050
const SimpleApp = createStackNavigator({...});
@@ -67,7 +67,6 @@ xcrun simctl openurl booted exp://127.0.0.1:19004/--/chat/Eric
6767
6868
```
6969

70-
7170
### Android
7271

7372
To test the intent handling in Android (Expo client app ), run the following:
@@ -87,7 +86,7 @@ Read the [Expo linking guide](https://docs.expo.io/versions/latest/guides/linkin
8786

8887
### URI Prefix
8988

90-
Next, let's configure our navigation container to extract the path from the app's incoming URI.
89+
Next, let's configure our navigation container to extract the path from the app's incoming URI.
9190

9291
```js
9392
const SimpleApp = createStackNavigator({...});
@@ -139,6 +138,7 @@ To test the URI on a real device, open Safari and type `mychat://chat/Eric`.
139138
To configure the external linking in Android, you can create a new intent in the manifest.
140139

141140
In `SimpleApp/android/app/src/main/AndroidManifest.xml`, do these followings adjustments:
141+
142142
1. Set `launchMode` of `MainActivity` to `singleTask` in order to receive intent on existing `MainActivity`. It is useful if you want to perform navigation using deep link you have been registered - [details](http://developer.android.com/training/app-indexing/deep-linking.html#adding-filters)
143143
2. Add the new `intent-filter` inside the `MainActivity` entry with a `VIEW` type action:
144144

@@ -154,7 +154,7 @@ In `SimpleApp/android/app/src/main/AndroidManifest.xml`, do these followings adj
154154
<action android:name="android.intent.action.VIEW" />
155155
<category android:name="android.intent.category.DEFAULT" />
156156
<category android:name="android.intent.category.BROWSABLE" />
157-
<data android:scheme="mychat" />
157+
<data android:scheme="mychat" />
158158
</intent-filter>
159159
</activity>
160160
```
@@ -180,3 +180,28 @@ const SimpleApp = createStackNavigator({...});
180180

181181
const MainApp = () => <SimpleApp enableURLHandling={false} />;
182182
```
183+
184+
Then, to handle the URL with the parameters, you can use `Linking` in your components to react to events.
185+
186+
```js
187+
componentDidMount() {
188+
// [...]
189+
Linking.addEventListener('url', this.handleDeepLink)
190+
}
191+
componentWillUnmount() {
192+
// [...]
193+
Linking.removeEventListener('url', this.handleDeepLink);
194+
}
195+
```
196+
197+
And the method to handle it :
198+
199+
```js
200+
handleDeepLink(e) {
201+
const route = e.url.replace(/.*?:\/\//g, '')
202+
// use route to navigate
203+
// ...
204+
}
205+
```
206+
207+
This should get you started! 🥳

website/versioned_docs/version-3.x/deep-linking.md

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,41 @@ In this guide we will set up our app to handle external URIs. Let's suppose that
1212
Previously, we had defined a navigator like this:
1313

1414
```js
15-
const SimpleApp = createAppContainer(createStackNavigator({
16-
Home: { screen: HomeScreen },
17-
Chat: { screen: ChatScreen },
18-
}));
15+
const SimpleApp = createAppContainer(
16+
createStackNavigator({
17+
Home: { screen: HomeScreen },
18+
Chat: { screen: ChatScreen },
19+
})
20+
);
1921
```
2022

2123
We want paths like `chat/Eric` to link to a "Chat" screen with the `user` passed as a param. Let's re-configure our chat screen with a `path` that tells the router what relative path to match against, and what params to extract. This path spec would be `chat/:user`.
2224

2325
```js
24-
const SimpleApp = createAppContainer(createStackNavigator({
25-
Home: { screen: HomeScreen },
26-
Chat: {
27-
screen: ChatScreen,
28-
path: 'chat/:user',
29-
},
30-
}));
26+
const SimpleApp = createAppContainer(
27+
createStackNavigator({
28+
Home: { screen: HomeScreen },
29+
Chat: {
30+
screen: ChatScreen,
31+
path: 'chat/:user',
32+
},
33+
})
34+
);
3135
```
3236

3337
If we have nested navigators, we need to provide each parent screen with a `path`. All the paths will be concatenated and can also be an empty string. This path spec would be `friends/chat/:user`.
3438

3539
```js
36-
const AuthSwitch = createAppContainer(createStackNavigator({
37-
AuthLoading: { screen: AuthLoadingScreen },
38-
App: {
39-
screen: AppStack,
40-
path: '',
41-
},
42-
Auth: { screen: AuthStack },
43-
}));
40+
const AuthSwitch = createAppContainer(
41+
createStackNavigator({
42+
AuthLoading: { screen: AuthLoadingScreen },
43+
App: {
44+
screen: AppStack,
45+
path: '',
46+
},
47+
Auth: { screen: AuthStack },
48+
})
49+
);
4450

4551
const AppStack = createStackNavigator({
4652
Home: { screen: HomeScreen },
@@ -73,7 +79,7 @@ You need to specify a scheme for your app. You can register for a scheme in your
7379

7480
### URI Prefix
7581

76-
Next, let's configure our navigation container to extract the path from the app's incoming URI.
82+
Next, let's configure our navigation container to extract the path from the app's incoming URI.
7783

7884
```js
7985
const SimpleApp = createAppContainer(createStackNavigator({...}));
@@ -96,7 +102,6 @@ xcrun simctl openurl booted exp://127.0.0.1:19004/--/chat/Eric
96102
97103
```
98104

99-
100105
### Android
101106

102107
To test the intent handling in Android (Expo client app ), run the following:
@@ -116,7 +121,7 @@ Read the [Expo linking guide](https://docs.expo.io/versions/latest/guides/linkin
116121

117122
### URI Prefix
118123

119-
Next, let's configure our navigation container to extract the path from the app's incoming URI.
124+
Next, let's configure our navigation container to extract the path from the app's incoming URI.
120125

121126
```js
122127
const SimpleApp = createAppContainer(createStackNavigator({...}));
@@ -168,6 +173,7 @@ To test the URI on a real device, open Safari and type `mychat://chat/Eric`.
168173
To configure the external linking in Android, you can create a new intent in the manifest.
169174

170175
In `SimpleApp/android/app/src/main/AndroidManifest.xml`, do these followings adjustments:
176+
171177
1. Set `launchMode` of `MainActivity` to `singleTask` in order to receive intent on existing `MainActivity`. It is useful if you want to perform navigation using deep link you have been registered - [details](http://developer.android.com/training/app-indexing/deep-linking.html#adding-filters)
172178
2. Add the new `intent-filter` inside the `MainActivity` entry with a `VIEW` type action:
173179

@@ -183,7 +189,7 @@ In `SimpleApp/android/app/src/main/AndroidManifest.xml`, do these followings adj
183189
<action android:name="android.intent.action.VIEW" />
184190
<category android:name="android.intent.category.DEFAULT" />
185191
<category android:name="android.intent.category.BROWSABLE" />
186-
<data android:scheme="mychat" />
192+
<data android:scheme="mychat" />
187193
</intent-filter>
188194
</activity>
189195
```
@@ -209,3 +215,28 @@ const SimpleApp = createAppContainer(createStackNavigator({...}));
209215

210216
const MainApp = () => <SimpleApp enableURLHandling={false} />;
211217
```
218+
219+
Then, to handle the URL with the parameters, you can use `Linking` in your components to react to events.
220+
221+
```js
222+
componentDidMount() {
223+
// [...]
224+
Linking.addEventListener('url', this.handleDeepLink)
225+
}
226+
componentWillUnmount() {
227+
// [...]
228+
Linking.removeEventListener('url', this.handleDeepLink);
229+
}
230+
```
231+
232+
And the method to handle it :
233+
234+
```js
235+
handleDeepLink(e) {
236+
const route = e.url.replace(/.*?:\/\//g, '')
237+
// use route to navigate
238+
// ...
239+
}
240+
```
241+
242+
This should get you started! 🥳

website/versioned_docs/version-4.x/deep-linking.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,28 @@ const SimpleApp = createAppContainer(createStackNavigator({...}));
218218

219219
const MainApp = () => <SimpleApp enableURLHandling={false} />;
220220
```
221+
222+
Then, to handle the URL with the parameters, you can use `Linking` in your components to react to events.
223+
224+
```js
225+
componentDidMount() {
226+
// [...]
227+
Linking.addEventListener('url', this.handleDeepLink)
228+
}
229+
componentWillUnmount() {
230+
// [...]
231+
Linking.removeEventListener('url', this.handleDeepLink);
232+
}
233+
```
234+
235+
And the method to handle it :
236+
237+
```js
238+
handleDeepLink(e) {
239+
const route = e.url.replace(/.*?:\/\//g, '')
240+
// use route to navigate
241+
// ...
242+
}
243+
```
244+
245+
This should get you started! 🥳

0 commit comments

Comments
 (0)