is there a way to tell if an iOS app is closed unexpectedly?(crash, force close)

前端 未结 3 971
悲&欢浪女
悲&欢浪女 2021-02-06 18:18

I want to do something on app start if the app is closed unexpectedly last time.(crash, force close), how can I tell it?

相关标签:
3条回答
  • 2021-02-06 18:33

    Ill post this just for future reference:

    For extra coverage check activity states with

    • (void)applicationDidBecomeActive:(UIApplication *)application
    • (void)applicationWillResignActive:(UIApplication *)application

    then use nsuserdefaults to keep track of the status

    the main difference between background checking is that these cover the edge case when the user immediately closes the app via app switcher when the app is still executing in the foreground. Also this helps for background compatible apps i think

    I think it might give you more coverage between a crash and a force close with these... although i haven't tested running out of battery

    0 讨论(0)
  • 2021-02-06 18:51

    Yes, you can do it: the simplest way is to create a file with a known name in the <Application_Home>/Documents/ directory, and delete that file upon the "normal" exit. If the file is there on launch, you know the previous exit has not been a "normal" one.

    You could update the content of the file periodically, writing the current timestamp on a timer every minute. This way you would know approximately when the abnormal exit has happened.

    0 讨论(0)
  • 2021-02-06 18:52

    I do this by setting a BOOL to YES in NSUserDefaults on app startup and when the app enters the foreground.

    I set this same value to NO when the app enters the background.

    At the start of didFinishLaunchingWithOptions (before setting the flag to YES), I check if the flag is currently set to YES. If it is YES, then I know the app crashed uncleanly.

    However, this does not check if the app was killed in the background (by the OS or the user).

    You can handle a force close while in the background (by the OS or the user) by setting a flag when the app enters the background and clearing the flag when the app enters the foreground. Then check for this flag in the didFinishLaunchingWithOptions method. If this flag is set then you know your app was killed while in the background.

    Keep in mind that a force close while in the background should not really be considered an "unexpected close". It's actually quite expected.

    0 讨论(0)
提交回复
热议问题