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

iOS应用中如何通过代码禁用‘Paste from Other Apps’权限弹窗?

禁用iOS应用的跨App粘贴权限弹窗及设置选项

核心思路

iOS 16+系统会在应用访问**系统通用剪贴板(UIPasteboard.general)**时触发权限请求弹窗,并在设置页面生成“Paste from Other Apps”选项。要禁用该功能,需避免应用访问系统通用剪贴板,改用自定义剪贴板或禁用自动粘贴行为。

具体实现方法

1. 替换为自定义剪贴板

如果应用仅需内部粘贴交互,无需跨App剪贴板功能,可创建应用专属的自定义剪贴板,完全替代系统通用剪贴板:

Swift 代码
// 在应用启动时初始化自定义剪贴板
let customPasteboard = UIPasteboard(name: UIPasteboard.Name(rawValue: "com.your-app-bundle-id.custom-pasteboard"), create: true)

// 复制文本示例
customPasteboard.string = "需要复制的内容"
// 粘贴文本示例
if let copiedText = customPasteboard.string {
    // 处理粘贴内容
}
Objective-C 代码
// 初始化自定义剪贴板
UIPasteboard *customPasteboard = [UIPasteboard pasteboardWithName:@"com.your-app-bundle-id.custom-pasteboard" create:YES];

// 复制文本示例
customPasteboard.string = @"需要复制的内容";
// 粘贴文本示例
NSString *copiedText = customPasteboard.string;
if (copiedText) {
    // 处理粘贴内容
}

2. 禁用内置UI控件的自动粘贴

UITextField、UITextView等控件默认会调用系统剪贴板实现自动粘贴,需自定义控件禁用该行为:

Swift 代码
class NoPasteTextField: UITextField {
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(paste(_:)) {
            return false
        }
        return super.canPerformAction(action, withSender: sender)
    }
}

class NoPasteTextView: UITextView {
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(paste(_:)) {
            return false
        }
        return super.canPerformAction(action, withSender: sender)
    }
}
Objective-C 代码
@interface NoPasteTextField : UITextField
@end

@implementation NoPasteTextField
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(paste:)) {
        return NO;
    }
    return [super canPerformAction:action withSender:sender];
}
@end

@interface NoPasteTextView : UITextView
@end

@implementation NoPasteTextView
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(paste:)) {
        return NO;
    }
    return [super canPerformAction:action withSender:sender];
}
@end

3. 移除系统剪贴板访问代码

全面排查项目中所有调用UIPasteboard.general的代码(如读取string/images属性、调用setString:等方法),全部替换为自定义剪贴板的使用逻辑。

效果验证

完成上述修改后,应用不会再触发跨App粘贴权限弹窗,系统设置页面中的“Paste from Other Apps”选项也会自动消失。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 01:52:04