ios SwiftUI检测用户何时进行屏幕截图或屏幕录制

thtygnil  于 11个月前  发布在  iOS
关注(0)|答案(2)|浏览(183)

UIViewController上,我们可以很容易地将观测器添加到控制器中。比如:

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

字符串
或捕获记录:

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


如何使用SwiftUI?

ifsvaxew

ifsvaxew1#

下面是一个简单的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")
            }
    }
}

字符串

k4aesqcs

k4aesqcs2#

现在你可能想要EnvironmentValues.isSceneCaptured
iOS 17及以后

相关问题