You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter app_links iOS深度链接无法触发路由跳转问题

iOS Flutter深度链接路由跳转失败问题

Android端已实现点击深度链接跳转App内指定页面,但iOS端存在异常:点击链接能正常打开App,却无法跳转到预设路由,Flutter的URI流监听无任何回调;不过AppDelegate调试时能正确捕获到点击的URL,目前不清楚遗漏了哪些配置或代码逻辑。

基础信息

  • 使用的深度链接包:app_links
  • 链接格式:https://mydomain/pub-crawl/{unique-id}
  • 已完成的配置:
    • 域名已添加到关联域(applinks:mydomain)
    • Info.plist中设置FlutterDeepLinkingEnabled为false,尝试过设为true,结果一致
    • 已在域名的.well-known目录下托管apple-app-site-association文件

相关代码

Flutter端初始化控制器代码

class SplashController extends GetxController {
  late final AppLinks appLinks;
  StreamSubscription? streamSub;

  @override
  void onReady() async {
    appLinks = AppLinks();
    await setupInitialStuff();
    super.onReady();
  }

  @override
  void onClose() {
    streamSub?.cancel();  // 清理流订阅
    super.onClose();
  }

  Future<void> handleIncomingLinks() async {
    // 监听App运行时的深度链接
    streamSub = appLinks.uriLinkStream.listen((Uri? uri) {
      if (uri != null) {
        deepLinkNavigation(uri);
      }
    }, onError: (err) {
      print('接收深度链接失败: $err');
    });
  }

  Future<void> deepLinkNavigation(Uri uri) async {
    // 提取路径前缀(如"pub-crawl"或"business")
    final prefix = uri.pathSegments.first;
    final uniqname = uri.pathSegments.last;

    // 延迟确保首页已加载完成
    await Future.delayed(Duration(milliseconds: 500));

    if (prefix == "pub-crawl") {
      await MyApplication.pubCrawlController.goToPubCrawlDeepLink(uniqname);
    } else if (prefix == "business") {
      await MyApplication.businessController.goToBusinessDeepLink(uniqname);
    } else {
      // 处理未知前缀或添加 fallback
      print('未知前缀: $prefix');
    }
  }
  
  Future<void> setupInitialStuff() async {
    // App启动时执行的初始化操作
    await handleIncomingLinks();
  }
}

iOS端AppDelegate.swift代码

import UIKit
import Flutter
import app_links

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)

    // 从启动参数中获取链接
    if let url = AppLinks.shared.getLink(launchOptions: launchOptions) {
      // 将链接传递给Flutter
      AppLinks.shared.handleLink(url: url)
      return true // 返回true会停止向其他包传递
    }

    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

apple-app-site-association文件内容

{
  "applinks": {
    "apps": [],
    "details": [
      {
        "appID": "MyTeamId.com.bundle.identifier",
        "paths": ["/pub-crawl/*", "/business/*"]
      }
    ]
  }
}

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 04:57:06