【问题标题】:"Would you like to save this password " dialog blocks keyboard from appearing“你想保存这个密码”对话框阻止键盘出现
【发布时间】:2019-10-08 12:35:28
【问题描述】:

我对“您要保存此密码”对话框有疑问。当它弹出并且用户转到主屏幕并返回应用程序时,对话框消失并且他无法在触摸文本字段时抬起键盘。它在 iOS 13 上唯一的“工作”。在 iOS 12 上它工作正常,因为当用户回到应用程序时,对话框仍然存在。然后他可以保存密码或现在不点击并开始输入。任何想法如何解决这个问题?它可能是某种 iOS 13 错误。

【问题讨论】:

  • 请向我们展示您的代码。
  • @Chris 没什么特别可看的。它只是带有电子邮件和密码字段以及登录按钮的 ViewController。当用户填写并按下登录时,他将被重定向到具有一次性密码的双因素授权屏幕,并弹出系统对话框“您要保存此密码以用于应用程序和网站吗?”。我没有控制它。它的基于 iOS 系统的对话框。当显示此对话框并且用户转到主屏幕并返回应用程序时,对话框消失并且在点击到两个因素文本字段后,键盘不显示。在 iOS 12 上没问题,因为对话框仍然存在。
  • 这个有什么更新吗?
  • @Lifeplus 您是否将委托分配给 textField?
  • 请显示示例代码。如果它是机密的,请在不透露机密细节的情况下制作一个演示该问题的示例项目。您可以通过多种方式实现诸如“他被重定向到两因素授权屏幕”之类的描述。这让每个人都更难猜测每个步骤是如何实施的。

标签: ios swift iphone


【解决方案1】:

问题

正如 OP 所写:如果您启用了关联域,则在 iOS 13 中使用自动填充功能,然后您会得到一个 UIAlertController 要求您保存或更新密码,请参阅https://developer.apple.com/videos/play/wwdc2017/206/ 了解更多信息。

iOS 13 的问题是,如果用户在点击“更新密码”或“不立即”按钮之前将应用程序置于后台,则在切换回前台后不再显示文本字段的键盘,看这里:

由于它是操作系统的系统对话框,因此您不能在进入后台之前以编程方式将其关闭。

因此,在 Apple 解决此问题之前,您可以:

  • SecRequestSharedWebCredential / SecAddSharedWebCredential 的解决方法
  • 或不使用该功能
  • 或忽略边缘情况

解决方法

解决方法可能是:

  • 不要在 iOS 13 上使用新的自动填充功能
  • 改用 SecRequestSharedWebCredential / SecAddSharedWebCredential

它可能看起来像这样:

不要使用自动填充

为了不使用新的自动填充,应该设置 textContentType,因此不设置:

userTextField.textContentType = .username
passwordTextField.textContentType = .password

也不要将 isSecureTextEntry 设置为 true。这意味着实际上您需要自己的机制来隐藏密码文本字段的条目。有关建议,请参见例如iOS 11 disable password autofill accessory view option?

SecRequestSharedWebCredential

在登录页面上可以在 viewDidLoad 中使用:

if #available(iOS 13, *) {
    requestCredentials()
} else {
    userTextField.textContentType = .username
    passwordTextField.textContentType = .password
}

private func requestCredentials() {
    SecRequestSharedWebCredential("software7.com" as CFString, nil, {
        credentials, error -> Void in
        guard error == nil else { return }
        guard let credentials = credentials, CFArrayGetCount(credentials) > 0 else { return }
        let unsafeCredential = CFArrayGetValueAtIndex(credentials, 0)
        let credential: CFDictionary = unsafeBitCast(unsafeCredential, to: CFDictionary.self)
        let dict: Dictionary<String, String> = credential as! Dictionary<String, String>
        let username = dict[kSecAttrAccount as String]
        let password = dict[kSecSharedPassword as String]

        DispatchQueue.main.async {
            self.userTextField.text = username;
            self.passwordTextField.text = password;
        }
    });
}

SecAddSharedWebCredential

在第二个 ViewController 的 viewDidLoad 中可以使用:

if #available(iOS 13, *) {
    updateCredentials()
} else {
    //works automatically with autofill
}

private func updateCredentials() {
    SecAddSharedWebCredential("software7.com" as NSString as CFString,
                              self.userName as NSString as CFString,
                              self.password as NSString as CFString,
                              { error in if let error = error { print("error: \(error)") }
    })
}

这看起来不如 iOS 13 的自动填充功能,但它们允许您在用户进入 bg/fg 时继续使用键盘,仍然提供自动填充和共享凭据。修复错误后,可以删除此解决方法。

【讨论】:

    【解决方案2】:
    // If you need to show another view controller before saving the password
    // you can simply zero out the content, save it inside the controller and
    // restore it when it is displayed again
    - (void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        self.loginTextField.text = self.login;
        self.passwordTextField.text = self.password;
    }
    
    - (void)viewWillDisappear:(BOOL)animated{
        [super viewWillDisappear:animated];
        self.login = self.loginTextField.text;
        self.password = self.passwordTextField.text;
        self.loginTextField.text = nil;
        self.passwordTextField.text = nil;
    }
    
    - (void)viewDidDisappear:(BOOL)animated{
        [super viewDidDisappear:animated];
        // System dialog "Save password...?" showed after this point
        // if .text of textField with contentType password is not nil
    
        // for save password manually use SecAddSharedWebCredential
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-04
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多