如何在 iOS 应用中禁用屏幕录制
How to disable screen Recording in iOS app
有什么方法可以关闭屏幕录制吗?还是可以通过配置文件实现?或者任何第三方库可用?
sharedRecorder.stopRecording( handler: { previewViewController, error in
if let error = error {
print("\(error.localizedDescription)")
}
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
previewViewController?.modalPresentationStyle = UIModalPresentationStyle.popover
previewViewController?.popoverPresentationController?.sourceRect = CGRect.zero
previewViewController?.popoverPresentationController?.sourceView = self.view
}
if previewViewController != nil {
self.previewViewController = previewViewController
previewViewController?.previewControllerDelegate = self
}
self.present(previewViewController!, animated: true, completion: nil)
})
return
}
https://developer.apple.com/documentation/replaykit/rpscreenrecorder/1620990-stoprecording
NotificationCenter.default.addObserver(self, selector: #selector(preventScreenRecording), name: NSNotification.Name.UIScreenCapturedDidChange, object: nil)
并在主视图中创建一个视图并防止那样。
(void) preventScreenRecording {
if (@available(iOS 11.0, *)) {
BOOL isCaptured = [[UIScreen mainScreen] isCaptured];
if (isCaptured) {
self.blockView.hidden = false;
}
else {
self.blockView.hidden = true;
}
}
当您的应用程序启动时,您可以测试 UIScreen.isCaptured
属性 并显示一些初始屏幕(如果它设置为 true
)。
您还应该观察(在某个地方订阅)capturedDidChangeNotification
notification,如果 UIScreen.isCaptured
设置为 true
,则执行相同的操作(显示启动画面) .
Swift 4岁以上
您只需在 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()
}
有什么方法可以关闭屏幕录制吗?还是可以通过配置文件实现?或者任何第三方库可用?
sharedRecorder.stopRecording( handler: { previewViewController, error in
if let error = error {
print("\(error.localizedDescription)")
}
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
previewViewController?.modalPresentationStyle = UIModalPresentationStyle.popover
previewViewController?.popoverPresentationController?.sourceRect = CGRect.zero
previewViewController?.popoverPresentationController?.sourceView = self.view
}
if previewViewController != nil {
self.previewViewController = previewViewController
previewViewController?.previewControllerDelegate = self
}
self.present(previewViewController!, animated: true, completion: nil)
})
return
}
https://developer.apple.com/documentation/replaykit/rpscreenrecorder/1620990-stoprecording
NotificationCenter.default.addObserver(self, selector: #selector(preventScreenRecording), name: NSNotification.Name.UIScreenCapturedDidChange, object: nil)
并在主视图中创建一个视图并防止那样。
(void) preventScreenRecording {
if (@available(iOS 11.0, *)) {
BOOL isCaptured = [[UIScreen mainScreen] isCaptured];
if (isCaptured) {
self.blockView.hidden = false;
}
else {
self.blockView.hidden = true;
}
}
当您的应用程序启动时,您可以测试 UIScreen.isCaptured
属性 并显示一些初始屏幕(如果它设置为 true
)。
您还应该观察(在某个地方订阅)capturedDidChangeNotification
notification,如果 UIScreen.isCaptured
设置为 true
,则执行相同的操作(显示启动画面) .
Swift 4岁以上
您只需在 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()
}