iOS 8: Remove sensitive information from views before moving to the background

拥有回忆 提交于 2019-12-21 05:13:10

问题


In iOS 7 my app presented an authentication screen when the app went into the background (by subscribing to UIApplicationDidEnterBackgroundNotification). The authentication controller removed sensitive information so the background screenshot did not show any user info. In iOS 8 this does not work anymore. The background screenshot now shows the view the user was last working in and not the authentication controller... even though when the app comes back into the foreground the authentication controller is active.

I found a work around for now. Instead of using UIApplicationDidEnterBackgroundNotification I can use name:UIApplicationWillResignActiveNotification however this causes a flash as the user leaves the app.

Is this a bug or did apple provide a new way to remove sensitive information from views before moving to the background.

Note: putting ignoreSnapshotOnNextApplicationLaunch in applicationWillResignActive: and applicationDidEnterBackground: did not help.

Update: created a bug report


回答1:


Similar approach to @Gurudev0777, but uses UIBlurEffect instead to obscure the content, and doesn't have the downside of worrying about different device screen metrics. Application delegate:

#define MY_BACKGROUND_SCREEN_TAG 1001//or any random but UNIQUE number.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Visual effect view for blur
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    [blurView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
    [blurView setFrame:self.window.frame];
    blurView.tag = MY_BACKGROUND_SCREEN_TAG;

    [self.window addSubview:blurView];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // remove blur view if present
    UIView *view = [self.window viewWithTag:MY_BACKGROUND_SCREEN_TAG];
    if (view != nil)
    {
        [UIView animateWithDuration:0.2f animations:^{
            [view setAlpha:0];

        } completion:^(BOOL finished) {
            [view removeFromSuperview];
        }];
    }
}

works like a charm...

Edited 5/18/2015: Thanks @Simeon Rice for observation about modal dialogs, revised to add blur view to self.window instead of rootViewController.view

Edited 8/23/2016: Thanks @tpankake for observation re: auto-resizing mask.




回答2:


- (void)applicationWillResignActive:(UIApplication *)application
{
   // show splash when app goto background
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.window.bounds];

    imageView.tag = 101; // assign image tag
    //    imageView.backgroundColor = [UIColor redColor];
    [imageView setImage:[UIImage imageNamed:@"Default.png"]];

    [UIApplication.sharedApplication.keyWindow.subviews.lastObject addSubview:imageView];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
// remove splash when app goto foreground
    UIImageView *imageView = (UIImageView *)[UIApplication.sharedApplication.keyWindow.subviews.lastObject viewWithTag:101]; // lookup image by image tag
    [imageView removeFromSuperview];
}



回答3:


here we are putting an imageview while the app animate to background -
-(void)applicationWillResignActive:(UIApplication *)application
{
    imageView = [[UIImageView alloc]initWithFrame:[self.window frame]];
    [imageView setImage:[UIImage imageNamed:@"Default@2x.png"]];
    [self.window addSubview:imageView];
}

Here is the code to remove the imageview:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    if(imageView != nil) {
        [imageView removeFromSuperview];
        imageView = nil;
    }
}
It is working and tested many times.
*** Please test this scenario into the device not in simulator.


来源:https://stackoverflow.com/questions/25794582/ios-8-remove-sensitive-information-from-views-before-moving-to-the-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!