IOS 防截屏实现

.h文件

objectivec 复制代码
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface HSNoScreenShotView : UIView

@end

NS_ASSUME_NONNULL_END

.m文件

objectivec 复制代码
#import "HSNoScreenShotView.h"

@interface HSNoScreenShotView ()

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

@end

@implementation HSNoScreenShotView

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

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

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

- (void)layoutSubviews {
    [super layoutSubviews];
    self.textField.frame = self.bounds;
    self.clearView.frame = self.bounds;

    if (self.textField.superview != self) {
        [self addSubview:self.textField];
    }
}

- (void)setupUI {
    [self addSubview:self.textField];
    self.textField.subviews.firstObject.userInteractionEnabled = YES;
    [self.textField.subviews.firstObject addSubview:self.clearView];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}


- (void)keyboardWillShow:(NSNotification *)noti {
    if (self.textField.isFirstResponder) {
        [self.textField resignFirstResponder];
        self.textField.subviews.firstObject.userInteractionEnabled = YES;
    }
}

- (void)addSubview:(UIView *)view {
    [super addSubview:view];
    if (self.textField != view) {
        [self.clearView addSubview:view];
    }
}

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

- (UIView *)clearView {
    if (!_clearView) {
        _clearView = [[UIView alloc] init];
        _clearView.backgroundColor = [UIColor clearColor];
    }
    return _clearView;
}

@end

使用逻辑,在你项目实现上面的防截图视图。将你不想被截图的视图添加为HSNoScreenShotView的子视图就行。

相关推荐
二流小码农2 天前
鸿蒙开发:搭建属于自己的私服共享包平台
android·ios·harmonyos
yinhaijing_ss2 天前
使用SPM为ios项目添加lookin所遇问题总结
ios
2401_865854882 天前
APP企业签名 苹果iOS企业签名
后端·ios·iphone
归辞...2 天前
「iOS」——知乎日报一二周总结
ios
解压专家6662 天前
2024 手机解压缩软件评测与推荐
ios·智能手机·winrar·7-zip
胖虎12 天前
CocoaPods安装步骤详解 - 2024
ios·cocoa·xcode·cocoapods
Smile_ping3 天前
uniapp iOS打包证书过期——重新下载证书及更新文件
ios·ios 证书、标识符和描述文件
青少儿编程课堂3 天前
iOS灵动岛动画小组件怎么播放动画
ios
小可以o3 天前
flutter 打包
android·flutter·ios