在Expo/React Native中如何配置Deep Link URL Schema实现深度链接?
Great question! You’re totally right that editing native files like info.plist isn’t feasible when using Expo—luckily, Expo provides streamlined, config-based ways to set up deep linking for both iOS and Android without touching native code. Let’s walk through the full process:
1. Define a URL Scheme
URL schemes are the widely used "direct" deep linking method (e.g., myapp://profile/123). To set this up in Expo:
- Open your project’s
app.json(orapp.config.jsif using dynamic configuration) - Add a
schemefield under theexpoobject:{ "expo": { "scheme": "myapp", // ... your existing config fields } }
This tells Expo to automatically configure both iOS and Android to recognize links starting with myapp://.
2. Set Up Universal Links (iOS) & App Links (Android)
For a more polished experience (using regular HTTPS links like https://yourapp.com/profile/123), you’ll want to set up Universal Links (iOS) and App Links (Android). Here’s how:
- Prepare your domain:
- For iOS: Upload an
apple-app-site-associationfile to your domain’s root (e.g.,https://yourapp.com/apple-app-site-association). This file needs to include your Expo app’s bundle ID. - For Android: Upload an
assetlinks.jsonfile tohttps://yourapp.com/.well-known/assetlinks.json, containing your app’s package name and SHA-256 fingerprint (you can grab this from Expo’s build dashboard after your first build).
- For iOS: Upload an
- Update your Expo config:
Add the associated domain settings toapp.json:{ "expo": { // ... other config "ios": { "associatedDomains": ["applinks:yourapp.com"] }, "android": { "intentFilters": [ { "action": "VIEW", "data": [ { "scheme": "https", "host": "yourapp.com" } ], "category": ["BROWSABLE", "DEFAULT"] } ] } } }
3. Handle Deep Links in Your App
Once the native config is set up, you need to process incoming links in your React Native code using the expo-linking library:
- Install the library first:
expo install expo-linking - Add link-handling logic to your root component (e.g.,
App.js):import { useEffect } from 'react'; import * as Linking from 'expo-linking'; import { NavigationContainer } from '@react-navigation/native'; import { createStackNavigator } from '@react-navigation/stack'; const Stack = createStackNavigator(); const handleDeepLink = (url) => { if (!url) return; // Parse the URL and navigate to the corresponding screen const parsedUrl = Linking.parse(url); if (parsedUrl.path.startsWith('/profile/')) { const userId = parsedUrl.path.split('/')[2]; // Navigate to your Profile screen with the userId } }; export default function App() { useEffect(() => { // Capture the link that launched the app Linking.getInitialURL().then(handleDeepLink); // Listen for new links while the app is running const linkSubscription = Linking.addEventListener('url', ({ url }) => handleDeepLink(url)); return () => linkSubscription.remove(); }, []); return ( <NavigationContainer> <Stack.Navigator> {/* Add your app's screens here */} </Stack.Navigator> </NavigationContainer> ); }
Quick Testing Tips
- For Expo Go: Run
expo start, then open a link likemyapp://testin your phone’s browser, or use the "Open in Expo Go" option from the Expo dev tools dashboard. - For standalone builds: After building with
expo build:iosorexpo build:android, test links on a physical device (simulators/emulators have limited support for deep linking).
内容的提问来源于stack exchange,提问作者Stretch0




