ios - 截取屏幕时显示警报并要求输入文本

标签 ios swift uialertview

想看看是否有办法在屏幕截图后显示警报 View ,然后要求输入与特定字符串匹配的文本。如果用户没有输入正确的文本,则不会关闭警报 View 并提示用户重试。如果用户输入了正确的文本,警报 View 将被关闭。

最佳答案

你可以用这个

let requireTextInput = "require text input"
// add observer for screen shot    
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationUserDidTakeScreenshot, object: nil, queue: OperationQueue.main, using:
        { notification in

            var inputTextField = UITextField()

            let textPrompt = UIAlertController(title: nil, message: "require text input", preferredStyle: .alert)

            textPrompt.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))

            textPrompt.addAction(UIAlertAction(title: "OK", style: .default, handler: {
                (action) -> Void in
            // if the input match the required text

                let str = inputTextField.text
                if str == requireTextInput {
                    print("right")
                } else {
                    print("wrong")
                }

            }))

            textPrompt.addTextField(configurationHandler: {(textField: UITextField!) in
                textField.placeholder = "place Holder"
                inputTextField = textField

            })

            self.present(textPrompt, animated: true, completion: nil)

    })

关于ios - 截取屏幕时显示警报并要求输入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41325820/

相关文章:

ios - 可以使用启动 Storyboard吗?

ios - NSInputStream 停止运行,有时会抛出 EXC_BAD_ACCESS

Swift:从 Firebase 数据库检索数据以进行标签

ios - 在 apple mapkit swift 上捏合和缩放

swift - 制作自定义 UINavigationBar

ios - 警报 addAction(action) 给我一条错误消息,指出操作是未解析的标识符

ios - 取消 MFMailComposeViewController 会导致内存泄漏?

iphone - 如何制作这样的自定义模式 uiview?

ios - 可以在iOS应用中使AlertView超时吗?

ios - UIAlertAction不显示传递的消息