美文网首页Swift
Swift 关闭 允许粘贴 弹窗

Swift 关闭 允许粘贴 弹窗

作者: 小奉不在乎 | 来源:发表于2024-05-16 16:02 被阅读0次

随着iOS 14的发布,剪切板的滥用也被大家所知晓。只要是APP读取剪切板内容,系统都会在顶部弹出提醒,而且这个提醒不能够关闭。这样,大家在使用APP的过程中就能够看到哪些APP使用了剪切板,但是每次进入都会弹一次,就会让人感觉很烦,下面是一个仅弹出提醒一次的实现方式,仅供参考

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        self.recognitionPasteboard()
        return true
}
func applicationDidBecomeActive(_ application: UIApplication) {
        self.recognitionPasteboard()
}

直接读取 更改为 UIPasteboard.general.detectPatterns 检测就可以轻松实现不弹出允许粘贴弹窗了

func recognitionPasteboard() {
    let status = UserDefaults.standard.bool(forKey: "UIPasteboardStatus")
    if status == false { copyVerify() }
    if #available(iOS 14.0, *) {
        let board = UIPasteboard.general
        // 设置粘贴板的检测模式
        UIPasteboard.general.detectPatterns(for: [.probableWebURL, .number, .probableWebSearch]) { result in
            switch result {
            case let .success(pattern):
                if pattern.contains(.probableWebURL) {
                    // 识别剪贴板中的内容
                    copyVerify()
                }
            case let .failure(error):
                printLog(error)
            }
        }
    }
    else {
        copyVerify()
    }
           
    func copyVerify() {
        // 识别剪贴板中的内容
        guard
            let paste = UIPasteboard.general.string
        else {
            return
        }
        UserDefaults.standard.set(true, forKey: "UIPasteboardStatus")
        UserDefaults.standard.synchronize()
               
        if !(paste.hasPrefix("通过吱吱云盘分享的文件") && paste.hasSuffix("复制这段内容打开“吱吱APP”即可获取")) {
            return
        }
        let pastes = paste.components(separatedBy: "\n")
        if let index = pastes.firstIndex(where: { $0.hasPrefix("链接:http://") || $0.hasPrefix("链接:https://") }) {
            // 如果剪贴板中的内容是链接
            UIPasteboard.general.string = nil
            DispatchQueue.main.async {
                let url = URL(string: UIApplication.openSettingsURLString)
                let alertController = UIAlertController(title: "温馨提示",
                                                        message: "符合xx的读取标准",
                                                        preferredStyle: .alert)
                alertController.addAction(UIAlertAction(title: "取消", style: .cancel))
                alertController.addAction(UIAlertAction(title: "确定", style: .default, handler: {
                    _ in
                    print(paste)
                }))
                UIViewController.topMost?.present(alertController)
            }
        }
    }
}

相关文章

网友评论

    本文标题:Swift 关闭 允许粘贴 弹窗

    本文链接:https://www.haomeiwen.com/subject/fctifjtx.html