工作生活

iOS Home键App切换时的隐私保护

2019-07-01  本文已影响0人  iOS_July
UC
T1-> 上图为UC 在APP切换时的一个保护效果,这个功能的作用域是:

当用户使用完app之后,一般会自然的切到后台,不会立即主动的Kill掉app,这种情况下,如果用户刚好停留在敏感数据页面,比如账户、余额、以及什么不可描述的页面,有旁人在的话,就会有点小尴尬啦,这时候就用得到该功能啦

T2-> 方案:
  1. 在AppDelegate的 applicationDidEnterBackground 代理方法中,给当前页面加上一层蒙板
  2. 然后在 applicationWillEnterForeground 代理方法中再移除蒙板.
T3->代码:
#define ProtectUserPrivacy @"ProtectUserPrivacy"
/** 隐私保护萌版*/
@property (nonatomic, strong) UIView *masksView;
- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    //查看是否开启隐私保护
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *protectStr = [userDefault objectForKey:ProtectUserPrivacy];
    if ([protectStr isEqualToString:@"1"]) {//如 开启了,则创建萌版
        
        UIView *masksView = [[UIView alloc]init];
        masksView.backgroundColor = [UIColor blackColor];
        masksView.frame = self.window.bounds;
        [self.window addSubview:masksView];
        self.masksView = masksView;
        
        UILabel *remindsLab = [[UILabel alloc]init];
        remindsLab.text = @"为您的隐私保驾护航!";
        remindsLab.textAlignment = NSTextAlignmentCenter;
        remindsLab.textColor = [UIColor whiteColor];
        remindsLab.font = [UIFont systemFontOfSize:20 weight:UIFontWeightBold];
        remindsLab.frame = CGRectMake(0, 100, self.window.bounds.size.width, 40);
        [masksView addSubview:remindsLab];
    }
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    
    //移除萌版
    [self.masksView removeFromSuperview];
}

- (IBAction)clickBtn:(UIButton *)sender {
    if (sender.tag == 0) {//打开保护
        
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        [userDefault setObject:@"1" forKey:ProtectUserPrivacy];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }else if (sender.tag == 1){//关闭
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        [userDefault setObject:@"0" forKey:ProtectUserPrivacy];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

T4->效果
close
open
上一篇下一篇

猜你喜欢

热点阅读