Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios SwiftUI检测用户何时拍摄屏幕截图或屏幕录制_Ios_Swift_Swiftui - Fatal编程技术网

Ios SwiftUI检测用户何时拍摄屏幕截图或屏幕录制

Ios SwiftUI检测用户何时拍摄屏幕截图或屏幕录制,ios,swift,swiftui,Ios,Swift,Swiftui,在UIViewController上,我们可以轻松地向控制器添加观察者。比如: 类ViewController:UIViewController{ 重写func viewDidLoad(){ super.viewDidLoad() NotificationCenter.default.addObserver(self,选择器:#选择器(didTakeScreenshot(通知:)),名称:UIApplication.userDidTakeScreenshotNotification,对象:nil

UIViewController
上,我们可以轻松地向控制器添加观察者。比如:

类ViewController:UIViewController{
重写func viewDidLoad(){
super.viewDidLoad()
NotificationCenter.default.addObserver(self,选择器:#选择器(didTakeScreenshot(通知:)),名称:UIApplication.userDidTakeScreenshotNotification,对象:nil)
}
@objc func屏幕截图(通知:通知){
打印(“截图”)
}
}
或通过以下方式捕获记录:

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

这里是一个简单的演示:

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")
            }
    }
}