app 进入后台进行模糊处理

金融类app防止信息在后台中被一些恶意截屏软件进行截屏,对进入后台的app做模糊处理

- (void)applicationWillResignActive:(UIApplication *)application {
    //进入后台盖上view,实现模糊效果
    
    UIView* view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //获取当前视图控制器
    UIViewController *vc = [self getCurrentVC];
    //截取当前视图为图片
    imageV.image = [self snapshot:vc.view];
    
    //添加毛玻璃效果
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
    effectview.frame = [UIScreen mainScreen].bounds;
    [imageV addSubview:effectview];
    view.tag = 1111;
    [view addSubview:imageV];
    
    [self.window addSubview:view];
}

//获取当前控制器

- (UIViewController *)getCurrentVC {
    
    UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
    if (!window) {
        return nil;
    }
    UIView *tempView;
    for (UIView *subview in window.subviews) {
        if ([[subview.classForCoder description] isEqualToString:@"UILayoutContainerView"]) {
            tempView = subview;
            break;
        }
    }
    if (!tempView) {
        tempView = [window.subviews lastObject];
    }
    
    id nextResponder = [tempView nextResponder];
    while (![nextResponder isKindOfClass:[UIViewController class]] || [nextResponder isKindOfClass:[UINavigationController class]] || [nextResponder isKindOfClass:[UITabBarController class]]) {
        tempView =  [tempView.subviews firstObject];
        
        if (!tempView) {
            return nil;
        }
        nextResponder = [tempView nextResponder];
    }
    return  (UIViewController *)nextResponder;
}

//截取当前视图为图片
- (UIImage *)snapshot:(UIView *)view

{
    
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
    
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    return image;
    
}

同时在app进入前台中需要把模糊界面去除

- (void)applicationDidBecomeActive:(UIApplication *)application {
    UIView *view = (UIView *)[self.window viewWithTag:1111];
    [view removeFromSuperview];
}

posted @ 2017-04-21 10:15  弋小木  阅读(2669)  评论(0编辑  收藏  举报