KMM iOS通过自定义URL启动应用无法获取参数求助
排查KMM iOS端自定义链接参数丢失问题
1. 修正Info.plist的URL配置细节
你的CFBundleTypeRole设置为Editor,该角色多用于文件编辑场景,针对自定义URL唤起场景,建议改为Viewer更适配:
<key>CFBundleTypeRole</key> <string>Viewer</string>
同时确认CFBundleURLName是唯一反向域名标识符、CFBundleURLSchemes里的iosstaging拼写无误差。
2. 检查AppDelegate方法正确性
确保application(_:open:options:)方法签名完全匹配,且逻辑正确:
func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { // 解析URL参数示例 let components = URLComponents(url: url, resolvingAgainstBaseURL: false) let userId = components?.queryItems?.first(where: { $0.name == "userId" })?.value print("Received userId: \(userId ?? "nil")") return true }
若你的AppDelegate继承自KMM生成的类,需确认方法被正确覆盖,未被父类实现拦截。
3. 兼容iOS 13+的Scene生命周期
即使未手动创建SceneDelegate,iOS 13及以上系统若检测到Info.plist存在UISceneConfiguration项,会默认启用Scene生命周期,此时AppDelegate的application(_:open:options:)不会触发。需添加SceneDelegate处理URL:
class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) { guard let url = URLContexts.first?.url else { return } // 解析URL参数逻辑同AppDelegate let components = URLComponents(url: url, resolvingAgainstBaseURL: false) let userId = components?.queryItems?.first(where: { $0.name == "userId" })?.value print("Received userId from Scene: \(userId ?? "nil")") } }
也可直接删除Info.plist中的UISceneConfiguration项,强制系统使用AppDelegate生命周期。
4. 处理冷启动时的launchOptions
冷启动唤起时,可通过didFinishLaunchingWithOptions获取URL,确认未遗漏该逻辑:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { if let url = launchOptions?[.url] as? URL { // 冷启动URL处理逻辑 let components = URLComponents(url: url, resolvingAgainstBaseURL: false) let userId = components?.queryItems?.first(where: { $0.name == "userId" })?.value print("Cold launch userId: \(userId ?? "nil")") } // 其他初始化逻辑 return true }
若此处launchOptions仍为nil,优先排查Info.plist的URL配置是否合规。
5. 排查框架拦截问题
针对Decompose和MVIKotlin框架:
- 确认AppDelegate中覆盖的生命周期方法未拦截事件转发给Decompose组件;
- 检查是否有第三方路由/链接处理库(如Firebase Dynamic Links)拦截自定义URL导致参数丢失。
6. 用命令行测试唤起逻辑
避免手动输入链接的误差,使用Xcode命令行工具验证:
xcrun simctl openurl booted iosstaging://open?userId=1234
通过控制台日志确认是否有URL参数输出。
内容的提问来源于stack exchange,提问作者Basti X
相关产品推荐
相关产品推荐

