Flutter Deeplink在Android正常但iOS无法跳转求助
Flutter iOS Deeplink 无法跳转问题排查与修复
一、检查iOS原生AppDelegate的DeepLink处理逻辑
iOS端获取DeepLink需要覆盖三种场景的处理方法,缺任一环节都可能导致链接无法传递:
- 冷启动时的DeepLink(通过
launchOptions获取) - 自定义URL Scheme打开App
- Universal Links打开App
以下是标准的处理实现,对比你的代码补全缺失部分:
import UIKit import Flutter @UIApplicationMain @objc class AppDelegate: FlutterAppDelegate { private var deepLinkChannel: FlutterMethodChannel? override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { GeneratedPluginRegistrant.register(with: self) // 初始化与Flutter通信的MethodChannel,确保名称与Flutter端一致 deepLinkChannel = FlutterMethodChannel(name: "your_app/deeplink", binaryMessenger: self.window?.rootViewController as! FlutterBinaryMessenger) // 处理冷启动时的DeepLink if let url = launchOptions?[.url] as? URL { _sendDeepLinkToFlutter(url: url) } return super.application(application, didFinishLaunchingWithOptions: launchOptions) } // 处理自定义URL Scheme打开App override func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { _sendDeepLinkToFlutter(url: url) return true } // 处理Universal Links打开App override func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { if userActivity.activityType == NSUserActivityTypeBrowsingWeb, let url = userActivity.webpageURL { _sendDeepLinkToFlutter(url: url) } return true } private func _sendDeepLinkToFlutter(url: URL) { // 打印日志确认是否拿到链接,方便排查 print("iOS端获取到DeepLink: \(url.absoluteString)") deepLinkChannel?.invokeMethod("handleDeepLink", arguments: url.absoluteString) } }
二、验证Flutter端Bloc的接收逻辑
确保Flutter端正确监听MethodChannel的回调,并触发_onRedirect对应的事件:
final _deepLinkChannel = MethodChannel('your_app/deeplink'); @override void onInit() { super.onInit(); // 监听原生传递的DeepLink _deepLinkChannel.setMethodCallHandler((call) async { if (call.method == 'handleDeepLink') { final String url = call.arguments as String; // 触发Bloc中处理跳转的事件,确保与_onRedirect绑定 add(DeepLinkReceivedEvent(url: url)); } }); }
检查点:
- MethodChannel名称与原生端完全一致
- 回调中是否正确发送了触发
_onRedirect的事件
三、核对iOS配置细节
即使已配置,再逐一确认以下项:
- 关联域名:Xcode中
Signing & Capabilities的Associated Domains添加applinks:你的域名(不要加http/https,比如applinks:example.com) - apple-app-site-association文件:确保文件放在域名的
.well-known目录下,可通过https://你的域名/.well-known/apple-app-site-association直接访问,内容格式正确:{ "applinks": { "apps": [], "details": [ { "appID": "你的TeamID.你的BundleID", "paths": ["*"] } ] } } - 自定义URL Scheme:Info.plist中添加
CFBundleURLTypes配置(如果用了自定义Scheme,比如yourapp://):<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>yourapp</string> </array> </dict> </array> - Enable Flutter Link:Xcode Build Settings中搜索
Enable Flutter Link,确保设为YES
四、分步测试定位问题
- 冷启动测试:完全关闭App,用Safari打开DeepLink链接,查看Xcode控制台是否打印
iOS端获取到DeepLink日志- 无日志:原生端未拿到链接,检查配置或AppDelegate方法实现
- 有日志但Flutter无反应:检查MethodChannel通信或Bloc事件绑定
- 热启动测试:App后台运行,打开DeepLink,重复上述排查
- 权限检查:iOS 14+首次打开链接会询问权限,若用户拒绝,需引导到
设置-你的App-允许打开链接开启权限
内容的提问来源于stack exchange,提问作者Mball
相关产品推荐
相关产品推荐

