简体   繁体   English

SwiftUI 检测用户何时截屏或录屏

[英]SwiftUI detect when the user takes a screenshot or screen recording

On UIViewController we can easy add observer to controller.UIViewController我们可以轻松地将观察者添加到控制器。 Like:喜欢:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(didTakeScreenshot(notification:)), name: UIApplication.userDidTakeScreenshotNotification, object: nil)
    }
    
    @objc func didTakeScreenshot(notification: Notification) {
        print("Screen Shot Taken")
    }
}

Or capturing record with:或使用以下方法捕获记录:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    let isCaptured = UIScreen.main.isCaptured
    return true
}

But how to do it with SwiftUI?但是如何使用 SwiftUI 做到这一点呢?

Here is a simple demo:这是一个简单的演示:

struct ContentView: View {
    @State var isRecordingScreen = false
    
    var body: some View {
        Text("Test")
            .onReceive(NotificationCenter.default.publisher(for: UIApplication.userDidTakeScreenshotNotification)) { _ in
                print("Screenshot taken")
            }
            .onReceive(NotificationCenter.default.publisher(for: UIScreen.capturedDidChangeNotification)) { _ in
                isRecordingScreen.toggle()
                print(isRecordingScreen ? "Started recording screen" : "Stopped recording screen")
            }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测用户何时从应用程序中截取屏幕截图? - How to detect when the user takes a screenshot out of the application? 如何使用ReplayKit屏幕录制API检测用户录制我的iOS应用程序? - How can I detect a user recording my iOS app with the ReplayKit screen recording APIs? 在 SwiftUI 中使用 List 时,有没有办法只检测屏幕上显示的单元格? - Is there a way to detect only the cells displayed on the screen when using List in SwiftUI? SwiftUI UIScrollView 当用户滚动到底部附近时检测 - SwiftUI UIScrollView Detect when user scrolls near the bottom 如何使用 Flutter 在 android 和 ios 中仅禁用屏幕录制(而不是屏幕截图)? - How to disable only screen recording (not screenshot) in android and ios with Flutter? SwiftUI:当用户从 longPressGesture 释放屏幕时如何运行操作? - SwiftUI: How to run action when user releases screen from a longPressGesture? 如何在flutter中检测用户截图动作? - How to detect user screenshot action in flutter? 在 SwiftUI 中检测并转发屏幕上任意位置的点击 - Detect and forward taps anywhere on screen in SwiftUI SwiftUI 检测 contextMenu 何时打开 - SwiftUI detect when contextMenu is open 在手机上,检测用户滚动“过去”屏幕顶部的时间 - On mobile phone, detect when user scrolls “past” top of screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM