赞
踩
UIApplicationUserDidTakeScreenshot
UIScreen
的isCaptured
方法可以捕获此屏幕状态变化,例如,录制、空中播放、镜像就会为真。 录屏通知名:UIScreenCapturedDidChange
,我们也可以监听录屏通知。import UIKit class ViewController: UIViewController { //监听屏幕状态 override func viewDidLoad() { super.viewDidLoad() //监听截屏通知 NotificationCenter.default.addObserver(self, selector: #selector(screenshots), name: NSNotification.Name.UIApplicationUserDidTakeScreenshot, object: nil) //监听录屏通知,iOS 11后才有录屏 if(UIDevice.current.systemVersion >= "11.0"){ //如果正在捕获此屏幕(例如,录制、空中播放、镜像等),则为真 if UIScreen.main.isCaptured { screenshots() } //捕获的屏幕状态发生变化时,会发送UIScreenCapturedDidChange通知,监听该通知 NotificationCenter.default.addObserver(self, selector: #selector(screenshots), name: NSNotification.Name.UIScreenCapturedDidChange, object: nil) } } //提示用户 @objc func screenshots(){ let alertvc = UIAlertController(title: "提示", message: "为保证用户名,密码安全,请不要截屏或录屏!", preferredStyle: UIAlertControllerStyle.alert) let action = UIAlertAction(title: "知道了", style: UIAlertActionStyle.default, handler: { (action) in alertvc.dismiss(animated: true, completion: nil) }) alertvc.addAction(action) self.present(alertvc, animated: true, completion: nil); } //析构函数中移除通知监听者 deinit { NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationUserDidTakeScreenshot, object: nil); if(UIDevice.current.systemVersion >= "11.0"){ NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIScreenCapturedDidChange, object: nil); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。