拍摄截屏时显示警报并要求输入文字

时间:2016-12-26 01:59:38

标签: ios swift uialertview

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

1 个答案:

答案 0 :(得分:2)

你可以用这个

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)

    })