You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS设备原生代码实现防截屏录屏功能问题求助

iOS原生实现防截屏与录屏的有效方案

你提供的基于UITextField的hack方法在iOS 13+版本中已失效,苹果调整了安全文本输入层的底层逻辑,导致该方法无法覆盖窗口内容。以下是两种可靠的原生实现方案:


方案一:使用官方isSecure属性(iOS 14+推荐)

苹果在iOS14后提供了原生窗口安全属性,可直接阻止截屏和录屏,效果稳定。

针对Scene架构(iOS13+主流配置)

修改SceneDelegate.swift:

import UIKit
import Flutter
import FirebaseCore

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        
        FirebaseApp.configure()
        let window = UIWindow(windowScene: windowScene)
        self.window = window
        
        // 开启窗口安全模式,直接阻止截屏/录屏
        windowScene.isSecure = true
        
        let flutterVC = FlutterViewController()
        window.rootViewController = flutterVC
        window.makeKeyAndVisible()
        GeneratedPluginRegistrant.register(with: flutterVC)
    }
}

针对旧版AppDelegate架构

修改AppDelegate.swift:

@main
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    FirebaseApp.configure()
    GeneratedPluginRegistrant.register(with: self)
    
    // 针对iOS14+开启窗口安全
    if let windowScene = self.window?.windowScene {
        windowScene.isSecure = true
    }
    
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }     
}

方案二:监听截屏/录屏通知(兼容iOS11+)

如果需要兼容低版本iOS,或者仅在特定页面开启防护,可以通过系统通知监听状态,动态添加遮挡层。

示例代码(FlutterViewController扩展)

import UIKit
import Flutter

class SecureFlutterVC: FlutterViewController {
    private var overlayView: UIView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 注册录屏状态变化、截屏完成通知
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(updateOverlayStatus),
            name: UIScreen.capturedDidChangeNotification,
            object: nil
        )
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(updateOverlayStatus),
            name: UIApplication.userDidTakeScreenshotNotification,
            object: nil
        )
        
        // 初始检查状态
        updateOverlayStatus()
    }
    
    @objc private func updateOverlayStatus() {
        if UIScreen.main.isCaptured {
            // 录屏中,显示黑色遮挡层(可替换为自定义提示图)
            showSecurityOverlay()
        } else {
            // 停止录屏,移除遮挡层
            hideSecurityOverlay()
        }
    }
    
    private func showSecurityOverlay() {
        guard overlayView == nil else { return }
        overlayView = UIView(frame: view.bounds)
        overlayView?.backgroundColor = .black
        overlayView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(overlayView!)
        view.bringSubviewToFront(overlayView!)
    }
    
    private func hideSecurityOverlay() {
        overlayView?.removeFromSuperview()
        overlayView = nil
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

方案对比

方案适用版本效果灵活性
isSecure属性iOS14+完全阻止截屏/录屏(截屏/录屏均为黑屏)全局生效,无法针对单个页面控制
通知监听+遮挡层iOS11+录屏时遮挡内容,截屏会有极短延迟(可能拍到部分内容)可针对特定页面开启,支持自定义遮挡样式

内容的提问来源于stack exchange,提问作者Naimish Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 07:38:09