radiospiel
radiospiel

Reputation: 2479

Preventing Screenshot/clearing cached data when iOS app gets backgrounded and resumed after a long time

I am building a cinema listing app, where the user can drill down thru the dataset to finally end up with a listing for a specific movie/theater/etc.

Now assume the user pauses using the app for 7 days. When reopening the app what he should not see are the listings from 7 days ago. But if the user just puts the app in background for a few minutes, the user should continue just where he left. I thought I could solve this issue by killing the app after a certain amount of time in background. This is the code:

static BOOL goingToQuit = NO;

#define KILL_IN_BACKGROUND_AFTER_SECS 300

- (void)applicationDidEnterBackground:(UIApplication *)application
{
  goingToQuit = YES;

  UIApplication* app = [UIApplication sharedApplication];
  UIBackgroundTaskIdentifier __block bgTask;
  bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
  }];

  if(UIBackgroundTaskInvalid != bgTask) {
    // Start the long-running task to kill app after some secs and return immediately.
    dispatch_after( dispatch_time(DISPATCH_TIME_NOW, KILL_IN_BACKGROUND_AFTER_SECS * 1e09), 
      dispatch_get_main_queue(), ^{
        if(goingToQuit) exit(0);
        [app endBackgroundTask: bgTask];
      });
  }
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
  // cancel ongoing background suicide.
  goingToQuit = NO;
}

What I see on my device is this: after KILL_IN_BACKGROUND_AFTER_SECS the app gets killed. After restarting the device logs show that the app got a new PID, entries showing the restart etc. Yet the device does not show the default.png startup image, but the screenshot of where the user has been before.

On the other hand if the user kills the application explicitely (double click on home button, tap & hold, click - on app) before he is restarting it the application starts with its default.png start up screen. This is the behaviour I want when killing the app programmatically.

Does anyone have an idea how to accomplish this? Any idea is highly appreciated.

BTW: As a workaround I tried to hide the main window during applicationDidEnterBackground and show it again on applicationWillEnterForeground. This, however, is highly confusing to the user when he is switching between apps.

Upvotes: 2

Views: 1955

Answers (1)

Matthew Frederick
Matthew Frederick

Reputation: 22305

In your app delegate's applicationDidEnterBackground you can display a view in front of the rest of your views, which the OS will grab as the last visible thing and which will be displayed when the app becomes active again (if it's still alive).

In your app delegate's applicationDidBecomeActive you can check to see if the data needs updating; if not then simply dismiss the view (animation is nice), and if so then first update (or just clear out) your data and then dismiss the view.

This is fairly common in apps. Many just use the default.png startup image, since users are accustomed to seeing it when the app launches normally.

Upvotes: 2

Related Questions