Search code examples
swiftuiscreenshotwkwebview

Unable to detect screenshot in simulator


I am trying to implement a screenshot and screen recording detection of a web view. After reading the answer to this question, I've written the following code:

struct WebContentView: View {

    @Binding var htmlName: String
    @State var isRecordingScreen = false

    var body: some View {
        Webview(htmlFileName: htmlName)
            .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")
            
            }
    }
}

However, when I run the app using the simulator and take a screenshot, nothing is being printed out.

What am I doing wrong?


Solution

  • Work fine with Xcode 12.5.1 and seems like you are taking screenshot direct from the save screen button. The save screen button is not a screenshot button.

    Take a screenshot from this command

    Device -> Trigger Screenshot

    enter image description here