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

iOS平台Objective-C防截屏:自定义白屏居中文字需求及问题

问题分析与解决方案

原代码问题说明

你当前的实现依赖UITextField.secureTextEntry的私有图层特性,这个特性本身会强制将对应图层渲染为黑色,因此无论你如何设置TextField的背景色、文字,最终截屏结果都会是黑屏。同时代码中的图层交换逻辑混乱,导致预期的文字视图无法被系统截屏捕获。

可行实现方案

换一种思路:通过创建全屏的白屏覆盖视图,在截屏动作发生或App进入后台时快速显示该视图,保证系统截屏/快照捕获到白屏加指定文字的内容,之后再隐藏视图恢复正常显示。

完整代码实现

@interface AppDelegate ()
@property (nonatomic, strong) UIView *captureBlockerView;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 初始化截屏拦截视图
    self.captureBlockerView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.captureBlockerView.backgroundColor = [UIColor whiteColor];
    self.captureBlockerView.hidden = YES;
    self.captureBlockerView.userInteractionEnabled = NO; // 不阻断用户交互
    
    // 添加居中文字标签
    UILabel *tipLabel = [[UILabel alloc] init];
    tipLabel.text = @"Prevent Capture";
    tipLabel.textColor = [UIColor blackColor];
    tipLabel.textAlignment = NSTextAlignmentCenter;
    tipLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightMedium];
    [self.captureBlockerView addSubview:tipLabel];
    
    // 设置自动布局
    tipLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [tipLabel.centerXAnchor constraintEqualToAnchor:self.captureBlockerView.centerXAnchor],
        [tipLabel.centerYAnchor constraintEqualToAnchor:self.captureBlockerView.centerYAnchor]
    ]];
    
    // 将视图添加到window最上层
    [self.window addSubview:self.captureBlockerView];
    [self.window bringSubviewToFront:self.captureBlockerView];
    
    // 监听截屏完成通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleScreenshotTaken)
                                                 name:UIApplicationUserDidTakeScreenshotNotification
                                               object:nil];
    
    // 监听App前后台切换,防止系统后台快照
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(showCaptureBlocker)
                                                 name:UIApplicationWillResignActiveNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(hideCaptureBlocker)
                                                 name:UIApplicationDidBecomeActiveNotification
                                               object:nil];
    
    return YES;
}

- (void)handleScreenshotTaken {
    // 显示拦截视图,延迟0.3秒隐藏(确保系统完成截屏捕获)
    self.captureBlockerView.hidden = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self hideCaptureBlocker];
    });
    
    // 弹出提示(可选,根据需求保留)
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                   message:@"禁止截屏"
                                                            preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
    [self.window.rootViewController presentViewController:alert animated:YES completion:nil];
}

- (void)showCaptureBlocker {
    self.captureBlockerView.hidden = NO;
}

- (void)hideCaptureBlocker {
    self.captureBlockerView.hidden = YES;
}

@end

方案优势

  1. 完全符合需求:截屏/后台快照会显示白屏加居中的Prevent Capture文字
  2. 不依赖私有API:所有实现基于公开的UIKit API,稳定性更高
  3. 覆盖场景全面:同时处理用户主动截屏和系统后台快照两种情况
  4. 不影响正常交互:拦截视图默认隐藏,仅在需要时显示,不干扰用户正常操作

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 15:10:21