Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a view or splash screen before applicationDidEnterBackground (to avoid active view screenshot)

I have confidential informations in my app, so I would like to hide them with a splash screen when the app is about to be moved to background.

I do run the app on iOS6 and further.

I tried to display the view in applicationWillResignActive but the problem is it display the splash screen even when user swipe control panel for example. I want it to show only when the app is moved to background.

I tried to displayed my splashScreen in applicationDidEnterBackground but it takes the screenShot before so informations are displayed at restoration during the animation.

Here the spirit of what I want :

- (void)applicationDidEnterBackground:(UIApplication *)application {     [_window addSubview:__splashController.view]; } 
like image 570
Tancrede Chazallet Avatar asked Nov 05 '13 15:11

Tancrede Chazallet


2 Answers

I think the problem is that you are testing in simulator. On device, it should work fine.

I tested this and it worked. Add an imageview with your splash image when app enters in background -

- (void)applicationDidEnterBackground:(UIApplication *)application {          UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.window.bounds];          imageView.tag = 101;    // Give some decent tagvalue or keep a reference of imageView in self     //    imageView.backgroundColor = [UIColor redColor];         [imageView setImage:[UIImage imageNamed:@"Default.png"]];   // assuming Default.png is your splash image's name          [UIApplication.sharedApplication.keyWindow.subviews.lastObject addSubview:imageView]; } 

And when app comes back in foreground -

- (void)applicationWillEnterForeground:(UIApplication *)application {     UIImageView *imageView = (UIImageView *)[UIApplication.sharedApplication.keyWindow.subviews.lastObject viewWithTag:101];   // search by the same tag value     [imageView removeFromSuperview];  } 

NOTE - On simulator (iOS 7.0), the added subview is not show when you check by pressing home button twice (Cmd + H), but on device it works as expected (like paypal, BofA apps)

EDIT: (Additional info)

In addition to obscuring/replacing sensitive information by adding subview / blur as explained above, iOS 7 provides you ability to ignore the screen snapshot via ignoreSnapshotOnNextApplicationLaunch of UIApplication inside applicationWillResignActive or applicationDidEnterBackground.

UIApplication.h

// Indicate the application should not use the snapshot on next launch, even if there is a valid state restoration archive. // This should only be called from methods invoked from State Preservation, else it is ignored. - (void)ignoreSnapshotOnNextApplicationLaunch NS_AVAILABLE_IOS(7_0); 

Also, allowScreenShot flag can be explored in Restrictions Payload.

like image 90
Ashok Avatar answered Sep 21 '22 09:09

Ashok


Swift 3.0 Answer for those who are to lazy to translate.

func applicationDidEnterBackground(_ application: UIApplication) {      let imageView = UIImageView(frame: self.window!.bounds)     imageView.tag = 101     imageView.image = ...      UIApplication.shared.keyWindow?.subviews.last?.addSubview(imageView)  }  func applicationWillEnterForeground(_ application: UIApplication) {      if let imageView : UIImageView = UIApplication.shared.keyWindow?.subviews.last?.viewWithTag(101) as? UIImageView {         imageView.removeFromSuperview()     }  } 
like image 21
SteffenK Avatar answered Sep 20 '22 09:09

SteffenK