How to remove cached files from an iOS application in case of an application crash

I save some documents (images and PDF) in my application in the iPad directory. Now these documents that are sensitive to the client should be deleted after the operation is completed, the user logs out. I handle the removal from the directory in the case of the "Logout" event, but how to do it if the application crashes.

+4
source share
3 answers

You need to add UncaughtExceptionHandler and remove caches.

void myHandler(NSException *exception)
{
  // Remove caches...
  .....
  // And maybe let the app to crash?
  exit(0);
}

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSSetUncaughtExceptionHandler(&myHandler);
     ....
}  

so myHandler is called when an unhandled NSException is thrown

+5
source

, . , , .

:

@interface AppDelegate()

void uncaughtExceptionHandler(NSException *exception);

@end


@implementation AppDelegate

void uncaughtExceptionHandler(NSException *exception) 
{
     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"app_did_crash"];
     [[NSUserDefaults standardUserDefaults] synchronize];
}

-(BOOL)application:(UIApplication *)application 
{
     // Get crash log
     NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
}
+1

I think you will need an exception handler using NSSetUncaughtExceptionHandler

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    NSSetUncaughtExceptionHandler(&myExceptionHandler);
}

void myExceptionHandler(NSException *exception)
{
    // do something before app crash here
}
0
source

All Articles