info.plist文件中添加
NSExtensionUsageDescription:此應(yīng)用需要訪問擴(kuò)展功能實(shí)現(xiàn)安全錄屏
實(shí)現(xiàn)方式一: UIScreenCapturedDidChangeNotification
錄屏開啟時(shí)规求,畫面黑屏,聲音可錄入
.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];
}
對于直播間內(nèi),涉及到 推拉流問題弊决,最好不要對聲道進(jìn)行處理
#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(@"安全錄屏配置音頻會(huì)話失敗:%@", exception.reason);
// [NetRequest postLog:[NSString stringWithFormat: @"安全錄屏配置音頻會(huì)話失敗:%@", 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(@"關(guān)閉安全錄屏");
[delegate hideSafeScreen];
}
}
實(shí)現(xiàn)方式二 具则,通過UITextField绪妹,將無法截屏的頁面添加至SafeView上
在UIViewContoller中
let secureTextField = SafeView(frame: .zero)
override func loadView() {
view = secureTextField
}
其中關(guān)于SafeView的完整實(shí)現(xiàn)代碼
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)
}
}