JayantBadlani / ScreenShield

ScreenShield is an iOS library that provides a simple way to protect your app's content from being captured or recorded by screenshots, screen recordings, or other screen capture methods. It works by adding a secure layer on top of your views, which prevents most screen capture mechanisms from recording the underlying content.
MIT License
113 stars 18 forks source link

In SwiftUI app the lib forces black color to safe areas #20

Open f3dm76 opened 2 months ago

f3dm76 commented 2 months ago

Attaching .protectScreenshot() to root view adds black lines

WindowGroup {
      RootView()
            .protectScreenshot() 
}

with .protectScreenshot() IMAGE 2024-09-10 16:31:52

without .protectScreenshot() Screenshot 2024-09-10 at 16 33 22

f3dm76 commented 2 months ago

And that is if I attach it further down the hierarchy - seems like it always replaces something with black

Screenshot 2024-09-10 at 16 38 28
MiaKoring commented 1 month ago

Try giving the View you want to protect a background, that seems to work for me example:

import SwiftUI
import ScreenShield

struct ContentView: View {
    @State var textHeight: CGFloat = 10
    var body: some View {
        VStack {
            Spacer()
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundStyle(.tint)

            ProtectedView {
                HStack {
                    Text("Hello, world!")

                }
                .background(.red)
            }

            Spacer()
        }
        .background(.gray)
        .padding()
    }
}

#Preview {
    ContentView()
}

struct ProtectedView<Content: View>: View {
    @ViewBuilder let content: () -> Content
    @State var contentDimensions: CGSize? = nil

    var body: some View {
        content()
            .background() {
                GeometryReader { geometry in
                    Spacer()
                        .onChange(of: geometry.size.height) {
                            contentDimensions = geometry.size
                        }
                }
            }
            .protectScreenshot()
            .frame(maxWidth: contentDimensions?.width, maxHeight: contentDimensions?.height)
    }
}