info.plist文件中添加
NSExtensionUsageDescription:此应用需要访问扩展功能实现安全录屏
实现方式一: UIScreenCapturedDidChangeNotification
录屏开启时,画面黑屏,声音可录入
.h
/// 安全录屏的黑色视图
@property (nonatomic, strong) UIView *safeScreenView;
.m
// 安全录屏
- (UIView *)safeScreenView {
if (!_safeScreenView) {
_safeScreenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH)];
[_safeScreenView setUserInteractionEnabled:NO];
_safeScreenView.backgroundColor = [UIColor blackColor];
[_safeScreenView setMultipleTouchEnabled:NO];
}
return _safeScreenView;
}
- (void)showSafeScreen {
[self.window addSubview:self.safeScreenView];
[self.window bringSubviewToFront:self.safeScreenView];
[self.safeScreenView setHidden: NO];
}
- (void)hideSafeScreen {
[self.safeScreenView setHidden: YES];
}
对于直播间内,涉及到 推拉流问题,最好不要对声道进行处理
#pragma mark- 安全录屏
- (void)handleSafeScreen {
// AVAudioSession * session = [AVAudioSession sharedInstance];
// @try {
// [session setCategory: AVAudioSessionCategoryPlayAndRecord mode:AVAudioSessionModeDefault options: AVAudioSessionCategoryOptionMixWithOthers error:nil];
// [session setActive:YES error:nil];
// } @catch (NSException *exception) {
// NSLog(@"安全录屏配置音频会话失败:%@", exception.reason);
// [NetRequest postLog:[NSString stringWithFormat: @"安全录屏配置音频会话失败:%@", exception.reason] isFailure:true];
// }
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(screenListenUpdate) name:UIScreenCapturedDidChangeNotification object:nil];
[self screenListenUpdate];
}
- (void)screenListenUpdate {
BOOL isSafeSwith = NO;
/// 0不可以录屏 1可以录屏
if (UIScreen.mainScreen.isCaptured && self.baseModel.user_extra_info != nil && self.baseModel.user_extra_info.is_screen_safe_switch == 0) {
isSafeSwith = YES;
}
AppDelegate *delegate = (AppDelegate *)UIApplication.sharedApplication.delegate;
if (isSafeSwith == YES) {
[delegate showSafeScreen];
NSLog(@"开启安全录屏");
} else {
NSLog(@"关闭安全录屏");
[delegate hideSafeScreen];
}
}
实现方式二 ,通过UITextField,将无法截屏的页面添加至SafeView上
在UIViewContoller中
let secureTextField = SafeView(frame: .zero)
override func loadView() {
view = secureTextField
}
其中关于SafeView的完整实现代码
class SafeView: UIView {
private var safeZone: UIView?
override init(frame: CGRect) {
super.init(frame: frame)
safeZone = makeSecureView(secureEnabled: true)
safeZone?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(safeZone!)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func makeSecureView(secureEnabled: Bool) -> UIView {
let tf = UITextField(frame: self.frame)
tf.isSecureTextEntry = secureEnabled
let secureView = tf.subviews.first
secureView?.isUserInteractionEnabled = true
secureView?.subviews.forEach { $0.removeFromSuperview() }
return secureView ?? UIView(frame: self.frame)
}
public override func addSubview(_ view: UIView) {
guard
let safe = safeZone,
view != safeZone
else {
super.addSubview(view)
return
}
safe.addSubview(view)
}
public override func insertSubview(_ view: UIView, belowSubview siblingSubview: UIView) {
guard
let safe = safeZone,
view != safeZone
else {
super.insertSubview(view, belowSubview: siblingSubview)
return
}
safe.insertSubview(view, belowSubview: siblingSubview)
}
public override func insertSubview(_ view: UIView, aboveSubview siblingSubview: UIView) {
guard
let safe = safeZone,
view != safeZone
else {
super.insertSubview(view, aboveSubview: siblingSubview)
return
}
safe.insertSubview(view, aboveSubview: siblingSubview)
}
public override func insertSubview(_ view: UIView, at index: Int) {
guard
let safe = safeZone,
view != safeZone
else {
super.insertSubview(view, at: index)
return
}
safe.insertSubview(view, at: index)
}
public override func exchangeSubview(at index1: Int, withSubviewAt index2: Int) {
guard
let safe = safeZone
else {
super.exchangeSubview(at: index1, withSubviewAt: index2)
return
}
safe.exchangeSubview(at: index1, withSubviewAt: index2)
}
public override func bringSubviewToFront(_ view: UIView) {
guard
let safe = safeZone,
view != safeZone
else {
super.bringSubviewToFront(view)
return
}
safe.bringSubviewToFront(view)
}
public override func sendSubviewToBack(_ view: UIView) {
guard
let safe = safeZone,
view != safeZone
else {
super.sendSubviewToBack(view)
return
}
safe.sendSubviewToBack(view)
}
}