替换应用进入后台时的截屏

为什么要做这件事儿?

当应用进入后台时,系统会自动在当前应用的页面截屏并存储到手机内,如果当前页面涉及敏感信息时,被攻击会造成泄密。

方案一 模糊背景图片

在应用被挂起时,在当前页面添加一层高斯模糊,在应用重新进入前台时,删除模糊效果。

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [super applicationDidEnterBackground:application];

     UIToolbar *bar =  [[UIToolbar alloc] initWithFrame:self.window.bounds];
     bar.barStyle = UIBarStyleDefault;
     bar.tag = 101;
    [UIApplication.sharedApplication.keyWindow.subviews.lastObject addSubview:bar];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [super applicationWillEnterForeground:application];

    UIView *view = (UIView *)[UIApplication.sharedApplication.keyWindow.subviews.lastObject viewWithTag:101];
    [view removeFromSuperview];
}

方案二 自定义背景图片

使用启动图作为进入后台的背景图片

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [super applicationDidEnterBackground:application];

    UIImage * img = [VRLaunchApi getSplashImg];
    imageView.tag = 101;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.window.bounds];
    [imageView setImage:img];
    [UIApplication.sharedApplication.keyWindow.subviews.lastObject addSubview:imageView];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [super applicationWillEnterForeground:application];

    UIImageView *imageView = (UIImageView *)[UIApplication.sharedApplication.keyWindow.subviews.lastObject viewWithTag:101];
    [imageView removeFromSuperview];
}

获取启动图片

+ (UIImage *)getSplashImg
{
    NSString *launchimg = @"";
    NSArray *imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
    CGSize viewSize = [UIScreen mainScreen].bounds.size;

    for (NSDictionary* dict in imagesDict)
    {
        CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
        if (CGSizeEqualToSize(imageSize, viewSize) && [@"Portrait" isEqualToString:dict[@"UILaunchImageOrientation"]])
        {
            launchimg = dict[@"UILaunchImageName"];
            break;
        }
    }
    
    DLog(@"getSplashImg:%@",launchimg);
    return [UIImage imageNamed:launchimg];
}

你可能感兴趣的:(替换应用进入后台时的截屏)