프로그래밍 언어/Flutter

[Flutter] 스크린샷 방지(Android) 및 검은색 화면으로 캡쳐(iOS) 방법

이로률 2025. 2. 28. 17:44

1. 안드로이드

"보안정책에 따라 화면을 캡처할 수 없어요." 토스트 메시지가 표출되면서 캡쳐 불가능하도록 구현

main.dart

@override
initState() {
    super.initState();   
    //스크린샷 방지
    if (Platform.isAndroid) {
        FlutterWindowManager.addFlags(FlutterWindowManager.FLAG_SECURE);
     }
     ...(코드생략)
 }

2. iOS

iOS는 캡쳐 자체를 막을 수 는 없기 때문에 스크린샷이 감지되었다는 토스트 메시지가 출력되는게 기본적이지만,
그것마저도 되지 않게 검은색 화면으로 캡쳐되도록 구현

AppDelegate.swift

import UIKit
import Flutter
import flutter_downloader
import WebKit
import Firebase
import FirebaseCore

@main
@objc class AppDelegate: FlutterAppDelegate {
  private var textField = UITextField()

  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    FirebaseApp.configure()

    UNUserNotificationCenter.current().delegate = self

    //스크린샷 방지
    self.window.makeSecure()

    GeneratedPluginRegistrant.register(with: self)
    FlutterDownloaderPlugin.setPluginRegistrantCallback(registerPlugins)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

...(코드 생략)

//스크린샷 방지
extension UIWindow {
    func makeSecure() {
        let field = UITextField()
        let view = UIView(frame: CGRect(x: 0, y: 0, width: field.frame.self.width, height: field.frame.self.height))
        field.isSecureTextEntry = true
        self.addSubview(field)
        self.layer.superlayer?.addSublayer(field.layer)
        field.layer.sublayers?.last!.addSublayer(self.layer)
        field.leftView = view
        field.leftViewMode = .always
    }
}

//스크린샷 방지 주석처리 된 부분만 코드 추가해주면 된다.