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

前端 未结 3 970
悲&欢浪女
悲&欢浪女 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: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.

提交回复
热议问题