简体   繁体   English

iOS:通过通用链接启动应用程序导致应用程序崩溃

[英]iOS: Launch App via universal link makes app crash

I am using Flutter to develop iOS app.我正在使用 Flutter 开发 iOS 应用程序。 When I launch the app via universal link (eg enter the url in Safari and click the OPEN button ), the app launches but will crash immediately.当我通过通用链接启动应用程序时(例如,在 Safari 中输入 url 并单击“打开”按钮),应用程序启动但会立即崩溃。 But if the app is already running on background, when I click the link, it will not crash.但是如果应用程序已经在后台运行,当我点击链接时,它不会崩溃。 When it crashes, I got the following log.当它崩溃时,我得到以下日志。 So I commented out all the code related to NSUserActivity, but I still got the same error.所以我把所有与NSUserActivity相关的代码都注释掉了,但是还是报了同样的错误。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (NSUserActivity)'

Any idea?任何想法? Thanks.谢谢。

I finally figured out the probelm, the bug is due to the failure of JSON conversion in didFinishLaunchingWithOptions function.我终于弄清楚了问题,该错误是由于didFinishLaunchingWithOptions function中的JSON转换失败所致。 I had a JSON conversion in one of my didFinishLaunchingWithOptions methods(serveral flutter plugins call override this method).我在我的一个 didFinishLaunchingWithOptions 方法(多个 flutter 插件调用覆盖此方法)中进行了 JSON 转换。 When enter the app via UL, a NSDictionary will be carried to the didFinishLaunchingWithOptions.通过 UL 进入应用时,会携带一个 NSDictionary 到 didFinishLaunchingWithOptions。 For instance, if enter from Safari, this NSDictionary will look like例如,如果从 Safari 输入,这个 NSDictionary 看起来像

dict :{
    UIApplicationLaunchOptionsSourceApplicationKey = "com.apple.mobilesafari";
    UIApplicationLaunchOptionsUserActivityDictionaryKey =     {
        UIApplicationLaunchOptionsUserActivityIdentifierKey = "ABCD-SOME-KEY";
        UIApplicationLaunchOptionsUserActivityKey = "<NSUserActivity: 0x12345678>";
        UIApplicationLaunchOptionsUserActivityTypeKey = NSUserActivityTypeBrowsingWeb;
    };
}

So when converting it to JSON, I get " uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (NSUserActivity)' ".因此,当将其转换为 JSON 时,我得到“未捕获的异常 'NSInvalidArgumentException',原因:'JSON 写入(NSUserActivity)中的类型无效'”。
I think what confused me is that from the log I thought the term "NSUserActivity" is something in my code (Since I do use this object in continueUserActivity method, and I had modified continueUserActivity to solve this problem before I got this bug), so I commented out these code but not wokring.我认为让我感到困惑的是,从日志中我认为术语“NSUserActivity”是我的代码中的东西(因为我确实在 continueUserActivity 方法中使用了这个 object,并且在我得到这个错误之前我已经修改了 continueUserActivity 来解决这个问题),所以我注释掉了这些代码,但没有注释掉。 However, I didn't notice it is a term inside a dictionary which could be created at runtime.但是,我没有注意到它是字典中的一个术语,可以在运行时创建。
My solution is to check the key in the dictionary before Json conversion and add try catch to avoid this bug.我的解决方案是在 Json 转换之前检查字典中的键并添加 try catch 以避免此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM