如果系统未禁止应用截屏,导致应用的关键信息可能会被截屏泄露,通过查看应用截图,可能获取到用户的敏感信息,造成个人资料外泄。
iOS实现不了不让截屏或者录屏,但是提供的截屏或者录屏的监听方法,当用户截屏或录屏时系统会发送相关通知,我们可以提示用户截屏或录屏会泄露一些个人安全信息,类似于微信或支付宝的付款码截屏。
监听通知
监听截屏通知
NotificationCenter.default.addObserver(self, selector: #selector(screenshots), name: UIApplication.userDidTakeScreenshotNotification, object: nil)
监听屏幕状态通知
NotificationCenter.default.addObserver(self, selector: #selector(screenCaptured), name: UIScreen.capturedDidChangeNotification, object: nil)
捕获屏幕状态
// 监听录屏通知,iOS 11后才有录屏
if #available(iOS 11.0, *) {
// 如果正在捕获此屏幕(例如,录制、空中播放、镜像等),则为真
if UIScreen.main.isCaptured {
screenCaptured()
}
}
提示方法
@objc func screenCaptured(){
let alertVC = UIAlertController(title: "提示", message: "为保证用户名,密码安全,请不要截屏或录屏!", preferredStyle: .alert)
let action = UIAlertAction(title: "知道了", style: .default, handler:nil)
alertVC.addAction(action)
self.present(alertVC, animated: true, completion: nil);
}
移除监听
deinit {
NotificationCenter.default.removeObserver(self, name: UIApplication.userDidTakeScreenshotNotification, object: nil);
NotificationCenter.default.removeObserver(self, name: UIScreen.capturedDidChangeNotification, object: nil);
}
添加水印
func createWaterMarkFor(Text strTxt:String,
andFullSize fsize:CGSize,
andCorners corners:UIRectCorner? = nil,
withRadius r:CGFloat? = nil) -> UIImage {
//[S] 1、设置水印样式
let paragraphStyle:NSMutableParagraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byWordWrapping
paragraphStyle.lineSpacing = 6.0 * 10
paragraphStyle.alignment = .center
var _attr:[NSAttributedString.Key:Any] = [
.font : UIFont.systemFont(ofSize: 15, weight: .semibold),
.foregroundColor:UIColor.init(red: 222/255.0, green: 222/255.0, blue: 222/255.0, alpha: 0.8),
.paragraphStyle: paragraphStyle,
.kern:1.0,
]
if #available(iOS 14.0, *) {
_attr[.tracking] = 1.0
}
let attributedString:NSMutableAttributedString = NSMutableAttributedString.init(string: strTxt)
let stringRange = NSMakeRange(0, attributedString.string.utf16.count)
attributedString.addAttributes(_attr,range: stringRange)
//[E]
//[S] 2、建立水印图
let _max_value = attributedString.size().width > attributedString.size().height ? attributedString.size().width : attributedString.size().height
let _size = CGSize.init(width: _max_value + 10, height: _max_value + 10)
//2.1、设置上下文
if UIScreen.main.scale > 1.5 {
UIGraphicsBeginImageContextWithOptions(_size,false,0)
}
else{
UIGraphicsBeginImageContext(_size)
}
var context = UIGraphicsGetCurrentContext()
//2.2、根据中心开启旋转上下文矩阵
//将绘制原点(0,0)调整到源image的中心
context?.concatenate(.init(translationX: _size.width * 0.8, y: _size.height * 0.4))
//以绘制原点为中心旋转45°
context?.concatenate(.init(rotationAngle: -0.25 * .pi))
//将绘制原点恢复初始值,保证context中心点和image中心点处在一个点(当前context已经发生旋转,绘制出的任何layer都是倾斜的)
context?.concatenate(.init(translationX: -_size.width * 0.8, y: -_size.height * 0.4))
//2.3、添加水印文本
attributedString.draw(in: .init(origin: .zero, size: _size))
//2.4、从上下文中获取水印图
let _waterImg = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage.init()
//[E]
//[S] 3、重设上下文,建立底图
if UIScreen.main.scale > 1.5 {
UIGraphicsBeginImageContextWithOptions(fsize,false,0)
}
else{
UIGraphicsBeginImageContext(fsize)
}
context = UIGraphicsGetCurrentContext()
//3.1圆角底图(可选)
if corners != nil && r != nil && r?.isNaN == false && r?.isFinite != false {
let rect:CGRect = .init(origin: .zero, size: fsize)
let bezierPath:UIBezierPath = UIBezierPath.init(roundedRect: rect,
byRoundingCorners: corners!,
cornerRadii: CGSize(width: r!, height: r!))
context?.addPath(bezierPath.cgPath)
}
//3.2 将水印图贴上去
var _tempC = fsize.width / _waterImg.size.width
var _maxColumn:Int = _tempC.isNaN || !_tempC.isFinite ? 1 : Int(_tempC)
if fsize.width.truncatingRemainder(dividingBy: _waterImg.size.width) != 0 {
_maxColumn += 1
}
_tempC = fsize.height / _waterImg.size.height
var _maxRows:Int = _tempC.isNaN || !_tempC.isFinite ? 1 : Int(_tempC)
if fsize.height.truncatingRemainder(dividingBy: _waterImg.size.height) != 0 {
_maxRows += 1
}
for r in 0..<_maxRows {
for c in 0..<_maxColumn {
let _rect:CGRect = .init(origin: .init(x: CGFloat(c) * _waterImg.size.width,
y: CGFloat(r) * _waterImg.size.height),
size: _waterImg.size)
_waterImg.draw(in: _rect)
}
}
//裁剪、透明
context?.clip()
context?.setFillColor(UIColor.clear.cgColor)
context?.fill(.init(origin: .zero, size: fsize))
//3.3 输出最终图形
let _canvasImg = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage.init()
//[E]
//4、关闭图形上下文
UIGraphicsEndImageContext()
return _canvasImg
}