IOS APP画面防截屏

首先声明下面的技术方案并非我的原创,也是通过百度搜索并验证是可行的,并把具体实现方式公布出来,希望可以帮到有同样需求的小伙伴门,共同进步~~

需求:为IOS自已开发的应用的所有画面添加防截屏、防录制的功能

解决依据是根据UITextField只要设置了setSecureTextEntry为true后,在进行录屏或者截屏的时候都会被系统隐去。

由于我们的app中即有非故事板画面又有故事板画面,所以只能一个画面一个画面的添加; 也可以创建一个非故事板类型的超类,来负责自动替换根UIView。

解决方案:

1、如果是非故事板类型的ViewController,在viewDidLoad方法开始处将self.view替换为防截屏的UIView,参见下面的示例代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // 防截屏
    UITextField *bgTextField = [[UITextField alloc] init];
    [bgTextField setSecureTextEntry:true];
      
    UIView *bgView = bgTextField.subviews.firstObject;
    [bgView setUserInteractionEnabled:true];
    self.view = bgView;

    // 其它初始化代码
}

2、如果是故事板类型的ViwController的,需要将故事板中将UIView改为SecurityScreenShortView(通过继承UIView后,在初始化方法中初始化防截屏安全区)即可,过程如下所示:
  1)、打开故事板设计画面;
  2)、选中相应ViewController的根UIView;
  3)、在最右侧设计器中将Class中的UIView改为SecurityScreenShortView后保存即可

SecurityScreenShortView.h

//
//  SecurityScreenShortView.h
//
//  Created by 杜燕军 on 2022/8/1.
//

#import 

NS_ASSUME_NONNULL_BEGIN
API_AVAILABLE(ios(13.2))
@interface SecurityScreenShortView : UIView

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIView *safeZone;

+ (UIView *)creactWithFrame:(CGRect)frame;
@end

NS_ASSUME_NONNULL_END

SecurityScreenShortView.m

//
//  SecurityScreenShortView.m
//
//  Created by 杜燕军 on 2022/8/1.
//

#import "SecurityScreenShortView.h"

@implementation SecurityScreenShortView

+ (UIView *)creactWithFrame:(CGRect)frame {
    return [[SecurityScreenShortView alloc] initWithFrame:frame];
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        [self addSafeZoneView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSafeZoneView];
    }
    return self;
}

- (void)addSafeZoneView {
    [self addSubview:self.safeZone];
    
    UILayoutPriority lowPriority = UILayoutPriorityDefaultLow - 1;
    UILayoutPriority heightPriority = UILayoutPriorityDefaultHigh - 1;

    self.safeZone.translatesAutoresizingMaskIntoConstraints = NO;
    [self.safeZone setContentHuggingPriority:lowPriority forAxis:UILayoutConstraintAxisVertical];
    [self.safeZone setContentHuggingPriority:lowPriority forAxis:UILayoutConstraintAxisHorizontal];
    [self.safeZone setContentCompressionResistancePriority:heightPriority forAxis:UILayoutConstraintAxisVertical];
    [self.safeZone setContentCompressionResistancePriority:heightPriority forAxis:UILayoutConstraintAxisHorizontal];
    
    [self addConstraints:@[
        [NSLayoutConstraint constraintWithItem:self.safeZone attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0],
        [NSLayoutConstraint constraintWithItem:self.safeZone attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0],
        [NSLayoutConstraint constraintWithItem:self.safeZone attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1 constant:0],
        [NSLayoutConstraint constraintWithItem:self.safeZone attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1 constant:0],
    ]];
}

- (void)addSubview:(UIView *)view {
    if (self.safeZone == view) {
        [super addSubview:view];
    }else{
        [self.safeZone addSubview:view];
    }
}

- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index {
    if (self.safeZone == view) {
        [super insertSubview:view atIndex:index];
    }else{
        [self.safeZone insertSubview:view atIndex:index];
    }
}

- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview {
    if (self.safeZone == view) {
        [super insertSubview:view aboveSubview:siblingSubview];
    }else{
        [self.safeZone insertSubview:view aboveSubview:siblingSubview];
    }
}

- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview {
    if (self.safeZone == view) {
        [super insertSubview:view belowSubview:siblingSubview];
    }else{
        [self.safeZone insertSubview:view belowSubview:siblingSubview];
    }
}

- (UITextField *)textField{
    if(!_textField){
        _textField = [[UITextField alloc]init];
        _textField.secureTextEntry = YES;
        _textField.enabled = NO;
    }
    return _textField;
}

- (UIView *)safeZone{
    if(!_safeZone){
      // 获取配置参数“是否开启防截屏”,默认值是0(未开启防截屏功能)此处不需要可以删除
      int noScreenShortEnable = [[ParamManager manager] getIntParam:@“noScreen” :0];
      if (noScreenShortEnable == 1) {
        // 防截屏
        _safeZone = self.textField.subviews.firstObject ?: [[UIView alloc] init];
        _safeZone.userInteractionEnabled = YES;
        for (UIView *v in _safeZone.subviews) {
            [v removeFromSuperview];
        }
      } else {
        // 非防截屏
        _safeZone = [[UIView alloc] init];
      }        
    }
    return _safeZone;
}

@end

故事板设置参照图

IOS APP画面防截屏_第1张图片

引用的方案链接:

1、简书-shyizne 【iOS防截屏-完全解决】

2、由于github故障的原因,所以故事板的链接找不到了; 

你可能感兴趣的:(IOS,ios,objective-c,xcode)