You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

在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 (or app.config.js if using dynamic configuration)
  • Add a scheme field under the expo object:
    {
      "expo": {
        "scheme": "myapp",
        // ... your existing config fields
      }
    }
    

This tells Expo to automatically configure both iOS and Android to recognize links starting with myapp://.

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:

  1. Prepare your domain:
    • For iOS: Upload an apple-app-site-association file 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.json file to https://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).
  2. Update your Expo config:
    Add the associated domain settings to app.json:
    {
      "expo": {
        // ... other config
        "ios": {
          "associatedDomains": ["applinks:yourapp.com"]
        },
        "android": {
          "intentFilters": [
            {
              "action": "VIEW",
              "data": [
                {
                  "scheme": "https",
                  "host": "yourapp.com"
                }
              ],
              "category": ["BROWSABLE", "DEFAULT"]
            }
          ]
        }
      }
    }
    

Once the native config is set up, you need to process incoming links in your React Native code using the expo-linking library:

  1. Install the library first:
    expo install expo-linking
    
  2. 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 like myapp://test in 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:ios or expo build:android, test links on a physical device (simulators/emulators have limited support for deep linking).

内容的提问来源于stack exchange,提问作者Stretch0

火山引擎 最新活动