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

Flutter项目中如何创建uni_link所需的唯一深度链接Scheme?

Hey there! Let's walk through setting up your custom myapp:// scheme for Flutter with uni_links, and clear up that website config question once and for all.

Since you're using a custom scheme (not App Links/Universal Links), you don't need any website configuration files—all setup happens directly in your Flutter project's native Android and iOS configs. Here's how to do it for each platform:

Android Configuration

  1. Open your project's android/app/src/main/AndroidManifest.xml file.
  2. Find your main Activity (usually named MainActivity). Add an <intent-filter> inside this activity to register your scheme:
<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <!-- This is your custom scheme - matches the "myapp" in myapp://mylink.com -->
    <data android:scheme="myapp" />
</intent-filter>

This tells Android that any link starting with myapp:// should trigger your app to open.

iOS Configuration

  1. Open ios/Runner/Info.plist in your project.
  2. Add the CFBundleURLTypes array to register your scheme:
<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>myapp</string>
        </array>
    </dict>
</array>

iOS will now recognize myapp:// links and launch your app when they're tapped.

Testing Your Scheme

Once you've added the configs, test it out to make sure it works:

  • Android: Run this command in your terminal (make sure your device/emulator is connected):
    adb shell am start -W -a android.intent.action.VIEW -d "myapp://mylink.com"
    
  • iOS: Open Safari on your simulator/device, type myapp://mylink.com, and hit enter—your app should open immediately.

In your Flutter code, use uni_links to handle the incoming links. Here's a quick example:

import 'package:uni_links/uni_links.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String? _receivedLink;

  @override
  void initState() {
    super.initState();
    _initUniLinks();
  }

  Future<void> _initUniLinks() async {
    // Handle the initial link that launched the app
    final initialLink = await getInitialLink();
    if (initialLink != null) {
      setState(() => _receivedLink = initialLink);
    }

    // Listen for links while the app is running
    linkStream.listen((String? link) {
      if (link != null) {
        setState(() => _receivedLink = link);
        // Add your logic here to navigate to the right screen based on the link
      }
    }, onError: (err) {
      // Handle any errors here
      debugPrint('Error listening to links: $err');
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Uni Links Demo')),
        body: Center(
          child: Text(_receivedLink ?? 'No link received yet'),
        ),
      ),
    );
  }
}

Do You Need a Website Configuration File?

Short answer: No!

Custom schemes like myapp:// are registered directly in your app's native config files (AndroidManifest.xml and Info.plist). They don't rely on your website at all—so you don't need to upload any config files (like assetlinks.json or apple-app-site-association) to your site, and HTTPS support isn't required. That's perfect for your situation!

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

火山引擎 最新活动