您只需在 Appdelegate.swift 页面中进行以下更改。
当用户尝试记录屏幕时,它将自动在应用程序顶部添加模糊视图。
weak var screen : UIView? = nil
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
NotificationCenter.default.addObserver(self, selector: #selector(preventScreenRecording), name: UIScreen.capturedDidChangeNotification, object: nil)
return true
}
@objc func preventScreenRecording() {
let isCaptured = UIScreen.main.isCaptured
print("isCaptured: \(isCaptured)")
if isCaptured {
blurScreen()
}
else {
removeBlurScreen()
}
}
func blurScreen(style: UIBlurEffect.Style = UIBlurEffect.Style.regular) {
screen = UIScreen.main.snapshotView(afterScreenUpdates: false)
let blurEffect = UIBlurEffect(style: style)
let blurBackground = UIVisualEffectView(effect: blurEffect)
screen?.addSubview(blurBackground)
blurBackground.frame = (screen?.frame)!
window?.addSubview(screen!)
}
func removeBlurScreen() {
screen?.removeFromSuperview()
}