[英]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.