iOS 12建议强密码文本字段委托回调用于选择我自己的密码

cnjp1d6j  于 2023-05-23  发布在  iOS
关注(0)|答案(6)|浏览(314)

在iOS 12中,我有一个新的密码文本字段用于注册流程,我希望系统建议一个强大的密码。我也有一个按钮,启用和禁用的基础上委托方法,我做了一些改变等。
textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String)
这对于在用户点击Use Strong Password时启用它很有用。但是当用户点击Choose My Own Password时,我似乎没有得到一个委托回调,结果我的按钮启用/禁用逻辑永远没有机会执行,允许某人用空白密码注册。

当用户点击Choose my own password时,我可能需要做些什么来获得回调?任何帮助都是非常感谢的。

yi0zb3m4

yi0zb3m41#

我也遇到了同样的问题,但我发现唯一提供回调的是UIViewviewDidLayoutSubviews委托。
所以我用了这个:

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        if passwordTextField.isFirstResponder {
           validatePassword(passwordTextField.text ?? "")
        }
    }
hc2pp10m

hc2pp10m2#

iOS 13引入了新的委托方法

func textFieldDidChangeSelection(_ textField: UITextField) {

}

当强密码被填充并删除时,将调用此函数。它仅在iOS 13中可用。
当文本更改或清除时也会调用它,因此请注意,如果您正在调用shouldChangeCharactersIn或使用UIControl.Event.editingChanged添加了一个操作,这两个方法都会响应

shyt4zoc

shyt4zoc3#

我不太熟悉“使用字符串密码”的委托,但是如果用户选择自己的密码,你需要检查用户在密码文本字段中输入的内容,并验证它是否符合你的标准。
为此,您需要使用其目标.editingChanged检查在密码textField中键入的内容。当用户键入任何内容时,它将启用或禁用按钮。看看步骤#3和步骤#4。第4步是什么将切换并决定枯萎启用或禁用按钮。
另一件需要注意的事情是,当vc第一次出现时,按钮应该是disabled,然后一旦密码textField数据有效,然后启用然后按钮。步骤#1和步骤#4
例如,在我的应用程序中,如果用户在密码textField内的所有空格中输入,则注册按钮将被禁用,但如果他们输入有效数据,则按钮将被启用。

更新我在viewDidLoad的步骤3A和3B中添加了,因为正如@DawsonToth在评论中正确指出的那样,如果用户选择了强密码,下一步按钮将被启用。但如果他们决定选择“选择我自己的密码”,则passwordTextField将被清除,但“下一步”按钮仍将启用。我没考虑到这点。

您需要将KVO observer添加到passwordTextField的“text”keypath,以便每次passwordTextField的文本更改时,handleTextInputChanged()将被调用,从而在文本被清除后禁用下一步按钮。

// 1. create a signUp button and make sure it is DISABLED and the color is .lightGray to signify that. In step 4 if the data inside the passwordTextField is valid I enable then button and change the color to blue
let signUpButton: UIButton = {
    let button = UIButton(type: .system)
    button.isEnabled = false // ** this must be set as false otherwise the user can always press the button **
    button.setTitle("SignUp", for: .normal)
    button.setTitleColor(UIColor.white, for: .normal)
    button.backgroundColor = UIColor.lightGray
    button.addTarget(self, action: #selector(signUpButtonTapped), for: .touchUpInside)
    return button
}()

// 2. create the password textField and set its delegate in viewDidLoad. For eg self.passwordTextField.delegate = self
let passwordTextField: UITextField = {
    let textField = UITextField()
    textField.placeholder = "Enter Password"
    textField.autocapitalizationType = .none
    textField.returnKeyType = .done
    textField.isSecureTextEntry = true

    // 3. add the target for .editingChanged to check the textField
    textField.addTarget(self, action: #selector(handleTextInputChanged), for: .editingChanged)
    return textField
}()

override func viewDidLoad() {
    super.viewDidLoad()

    passwordTextField.delegate = self // if this is programmatic make sure to add UITextFieldDelegate after the class name

    // 3A. Add a KVO observer to the passwordTextField's "text" keypath
    passwordTextField.addObserver(self, forKeyPath: "text", options: [.old, .new], context: nil)
}

// 3B. call the observer
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "text" {
        handleTextInputChanged()
    }
}
// 4. this checks what's typed into the password textField from step 3
@objc fileprivate func handleTextInputChanged() {

    let isFormValid = !isPasswordTextFieldIsEmpty() // if the textField ISN'T empty then the form is valid

    if isFormValid {

        signUpButton.isEnabled = true
        signUpButton.backgroundColor = .blue
    } else {

       signUpButton.isEnabled = false
       signUpButton.backgroundColor = .lightGray
    }
}

// 5. create a function to check to see if the password textField is empty
func isPasswordTextFieldIsEmpty() -> Bool {

    // 6. this checks for blank space
    let whiteSpace = CharacterSet.whitespaces

    // 7. if the passwordTextField has all blank spaces in it or is empty then return true
    if passwordTextField.text!.trimmingCharacters(in: whitespace) == "" || passwordTextField.text!.isEmpty {
        return true
    }
    return false // if it has valid characters in it then return false
}

// 8. target method from step 1
@objc func signUpButtonTapped() {
    // run firebase code
}
dxpyg8gm

dxpyg8gm4#

我也遇到了同样的问题。我所做的只是计算文本字段的子视图。

if textField.subviews.count == 3 { } // Has suggested password

我来办理入住手续

@objc func textFieldDidChange(_ textField: UITextField)
bprjcwpo

bprjcwpo5#

我通过继承UITextField并确定子视图是否包含UIKBASPCoverView来解决这个问题。

open class TextField: UITextField {
     private var hasAutomaticStrongPassword: Bool = false {
         didSet {
             if oldValue && !hasAutomaticStrongPassword {
                 // After user selecting "Choose My Own Password"
                 // Manually triggering changes
                 sendActions(for: .editingChanged)
                 _ = delegate?.textField?(self, shouldChangeCharactersIn: NSRange(location: 0, length: 0), replacementString: "")
             }
         }
     }
      
     open override func layoutSubviews() {
         super.layoutSubviews()
            
         hasAutomaticStrongPassword = self.subviews.first { subview in
              NSStringFromClass(subview.classForCoder) == "UIKBASPCoverView"
         } != nil
     }
 }
laawzig2

laawzig26#

我也面临着同样的问题。
我通过监听onEndEditing事件来更新状态。

<Input value={this.state.val} 
  onChangeText={(val) => this.setState({val})} 
  secureTextEntry={true} 
  onEndEditing={(e)=>{this.setState({val : e.nativeEvent.text)}}/>

相关问题