在直播app开发中怎么样实现防止画面截屏

在今天的数字时代,直播已成为我们生活中不可缺少的一部分,用户看直播从中找到乐趣,可以购买喜欢的东西,公司通过直播来扩大自己的影响力,在这个网络飞速发展的时代,为防止画面截屏在直播app开发中怎么样实现

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

 一、确定直播app开发的需求

需求:在直播app开发中所有画面添加防截屏、防录制的功能

解决依据是根据UITextField只要设置了setSecureTextEntry为true后,在进行录屏或者截屏的时候都会被系统隐去。由于我们直播app开发中即有非故事板画面又有故事板画面,所以只能一个画面一个画面的添加; 也可以创建一个非故事板类型的超类,来负责自动替换根UIView。

二、直播app开发的解决方案:

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

1
2
3
4
5
6
7
8
9
10
11
12
- (void)viewDidLoad {
    [super viewDidLoad];
    // 防截屏
    UITextField *bgTextField = [[UITextField alloc] init];
    [bgTextField setSecureTextEntry:true];
       
    UIView *bgView = bgTextField.subviews.firstObject;
    [bgView setUserInteractionEnabled:true];
    self.view = bgView;
  
    // 其它初始化代码
}

  

2、直播app开发中如果是故事板类型的ViwController的,需要将故事板中将UIView改为SecurityScreenShortView

(通过继承UIView后,在初始化方法中初始化防截屏安全区)即可,过程如下所示:

  1)、打开故事板设计画面;

  2)、选中相应ViewController的根UIView;

  3)、在最右侧设计器中将Class中的UIView改为SecurityScreenShortView后保存即可

SecurityScreenShortView.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//
//  SecurityScreenShortView.h
//
//  Created by 杜燕军 on 2022/8/1.
//
  
#import <UIKit/UIKit.h>
  
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
//  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

故事板设置参照图

引用的方案链接:

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

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

以上就是 为防止画面截屏在直播app开发中怎么样实现,更多内容欢迎关注之后的文章。

 

posted @   云豹科技-苏凌霄  阅读(139)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2022-08-09 直播间搭建,按钮左滑出现删除等操作按钮
2022-08-09 直播app开发搭建,flutter 实现自适应、自动换行、相对布局
2022-08-09 直播平台怎么搭建,原生js实现编辑器撤消/恢复功能
2021-08-09 短视频app搭建获取手机图库最新一个视频或照片的缩略图
2021-08-09 实现直播带货系统开发通知栏显示下载图片的进度条
2021-08-09 XML动态加载如何在直播视频网站源码中实现
点击右上角即可分享
微信分享提示