Xcode 12.2 安全密码文本字段不良行为

Posted

技术标签:

【中文标题】Xcode 12.2 安全密码文本字段不良行为【英文标题】:Xcode 12.2 Secure Password Text Field undesired behavior 【发布时间】:2020-11-26 00:52:23 【问题描述】:

我创建了一个注册视图控制器,其中包含用于电子邮件、用户名和密码的 3 个文本字段。这是整个视图控制器文件:

//
//  SignupViewController.swift
//  bounce_frontend
//
//  Created by Sebastian Fay on 11/21/20.
//

import UIKit

class SignupViewController: UIViewController 
    
    struct Constants 
        static let cornerRadius: CGFloat = 5.0
    
    
    private let emailField: UITextField = 
        let field = UITextField()
        field.placeholder = "Email"
        field.returnKeyType = .next
        field.leftViewMode = .always
        field.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 0))
        field.autocapitalizationType = .none
        field.autocorrectionType = .no
        field.layer.masksToBounds = true
        field.layer.cornerRadius = Constants.cornerRadius
        field.backgroundColor = .secondarySystemBackground
        field.layer.borderWidth = 1.0
        field.layer.borderColor = UIColor.secondaryLabel.cgColor
        return field
    ()
    
    private let usernameField: UITextField = 
        let field = UITextField()
        field.placeholder = "Username"
        field.returnKeyType = .next
        field.leftViewMode = .always
        field.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 0))
        field.autocapitalizationType = .none
        field.autocorrectionType = .no
        field.layer.masksToBounds = true
        field.layer.cornerRadius = Constants.cornerRadius
        field.backgroundColor = .secondarySystemBackground
        field.layer.borderWidth = 1.0
        field.layer.borderColor = UIColor.secondaryLabel.cgColor
        return field
    ()
    
    private let passwordField: UITextField = 
        let field = UITextField()
        field.isSecureTextEntry = true
        field.textContentType = .oneTimeCode
        field.placeholder = "Password"
        field.returnKeyType = .next
        field.leftViewMode = .always
        field.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 0))
        field.autocapitalizationType = .none
        field.autocorrectionType = .no
        field.layer.masksToBounds = true
        field.layer.cornerRadius = Constants.cornerRadius
        field.backgroundColor = .secondarySystemBackground
        field.layer.borderWidth = 1.0
        field.layer.borderColor = UIColor.secondaryLabel.cgColor
        return field
    ()
    
    private let createAccountButton: UIButton = 
        let button = UIButton()
        button.setTitle("Create account", for: .normal)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = Constants.cornerRadius
        button.backgroundColor = .systemRed
        button.setTitleColor(.white, for: .normal)
        return button
    ()
    

    override func viewDidLoad() 
        super.viewDidLoad()
        
        createAccountButton.addTarget(self, action: #selector(didTapCreateAccountButton), for: .touchUpInside)
        
        emailField.delegate = self
        usernameField.delegate = self
        passwordField.delegate = self
        
        addSubviews()
        view.backgroundColor = .systemBackground
        // Do any additional setup after loading the view.
    
    
    private func addSubviews() 
        view.addSubview(emailField)
        view.addSubview(usernameField)
        view.addSubview(passwordField)
        view.addSubview(createAccountButton)
    
    
    override func viewDidLayoutSubviews() 
        super.viewDidLayoutSubviews()
        
        emailField.frame = CGRect(x: 20, y: view.safeAreaInsets.top + 100, width: view.width-40, height: 52)
        usernameField.frame = CGRect(x: 20, y: emailField.bottom + 10, width: view.width-40, height: 52)
        passwordField.frame = CGRect(x: 20, y: usernameField.bottom + 10, width: view.width-40, height: 52)
        createAccountButton.frame = CGRect(x: 20, y: passwordField.bottom + 10, width: view.width-40, height: 52)
    
    
    @objc private func didTapCreateAccountButton() 
        print("creating account")
        emailField.resignFirstResponder()
        usernameField.resignFirstResponder()
        passwordField.resignFirstResponder()
        
        guard let userEmail = emailField.text, !userEmail.isEmpty, let userUsername = usernameField.text, !userUsername.isEmpty, let userPassword = passwordField.text, !userPassword.isEmpty, userPassword.count >= 8 else 
            return
        
        
        // check if the email and username are available
        AuthManager.shared.emailUsernameAvailable(email: userEmail, username: userUsername)  areAvailable in
            if areAvailable 
                print("username and email are available")
             else 
                print("username and email are NOT available")
            
        
    



extension SignupViewController: UITextFieldDelegate 
    func textFieldShouldReturn(_ textField: UITextField) -> Bool 
        if textField == emailField 
            usernameField.becomeFirstResponder()
         else if textField == usernameField 
            passwordField.becomeFirstResponder()
         else if textField == passwordField 
            didTapCreateAccountButton()
        
        return true
    

它们的布局如下: fresh view controller

在模拟器中,当我点击密码文本字段时,文本字段变为黄色并显示“强密码”,同时隐藏输入的文本:。 text typed into password field

但是,我也意识到,如果我在电子邮件和用户名 textField 之前单击密码 textField,则 textField 会按预期运行: text typed into password field before others

我读过其他帖子说 XCode 推断您的视图的目的可能是从文件名和变量名注册,这可能导致错误,但我无法找到针对这种特定情况的修复.谢谢!

【问题讨论】:

【参考方案1】:

我的第一印象是您的视图控制器中有一个具有副作用的逻辑。过去你的控制器代码。

覆盖您的文本字段内容的附加“密码”标签是预期行为,因为您激活了名为密码自动填充的选项。

要完全激活此功能,您必须启用应用的关联域。

如果不可能,那么只需将 textContentType 设置为 .password 并将 isSecureTextEntry 设置为 true。

【讨论】:

以上是关于Xcode 12.2 安全密码文本字段不良行为的主要内容,如果未能解决你的问题,请参考以下文章

用户登录后清除密码文本字段

本机 UITextField 安全文本输入强制使用英语(美国)键盘

Xcode Swift 5 MacOs 应用程序存档时为空窗口,但在从 Xcode 运行时有效

安全文本条目不起作用

JavaMail 保存密码

如果你的博客被封号了?