IOS中的自动OTP验证?

Rin*_*ath 19 iphone sms one-time-password ios ios12

有没有办法从iPhone收件箱(SMS)访问数据到ios应用程序进行自动OTP验证,如Android中的那样?我将非常感谢你的帮助.

iVa*_*run 17

在iOS 12中,Apple推出了一个名为的功能Security Code AutoFill.

要在您的应用程序中使用它,您只需设置UITextField输入视图的textContentType属性即可oneTimeCode.

otpTextField.textContentType = .oneTimeCode
Run Code Online (Sandbox Code Playgroud)

注意:安全代码自动填充仅适用于系统键盘,它不适用于自定义键盘.

WWDC视频

当你得到OTP时,它看起来像这样:

在此输入图像描述


iVe*_*tis 12

您收到的短信包含带有“代码”的内容也很重要,例如

“您的密码是:123456”

或者

“12345是你的登录密码”

沿着这条线的东西。

不是!

您的应用:12345

您可以通过点击消息中带下划线的代码来验证文本消息中的代码是否适用于 .oneTimeCode 类型。如果弹出一个对话框,上面写着“复制代码”,你就可以开始了。否则,您可能需要更改消息的文本。


Fa.*_*uri 9

要在文本字段上获取 otp,需要注意两件事

\n

1.将输入字段设置为otp

\n

其他答案让您了解如何管理代码以接收 otp。

\n

2. 在您的消息中提供“代码”短语

\n

您的消息必须包含英文的“代码”、“密码”、“密码”,如果您想在其他语言测试中启用它,请将一些单词从“代码”翻译成您的语言

\n

我收到了带有以下内容的消息:

\n

西班牙语:(Codingo)

\n

德国:(科德)

\n

捷克语:(Kod)

\n

波斯语:(\xd8\xb1\xd9\x85\xd8\xb2)

\n

阿拉伯语:(\xd8\xb1\xd9\x85\xd8\xb2)

\n

OTP 号码必须是英文,而不是您的语言号码,您的“代码”短语必须与您的号码仅用一个空格分隔,或者用“:”分隔,不带空格。

\n

代码 1111111

\n

代码:111111

\n

您必须注意的另一件重要事情是消息预览。如果下面有灰线,则该数字将被检测为 otp。如果它显示为蓝色或没有线条,则消息不会以 otp 形式通知您。类似图像的东西

\n

在此输入图像描述

\n


El0*_*din 8

在 Xamarin iOS 中,对于 >=iOS 12:

首先,短信需要在其消息中包含关键字“代码”或“密码”,并且代码后不要使用空格。如果您收到短信并且您有“复制代码”按钮,那么它将起作用

在此处输入图片说明

然后你需要放置这个:

_txtField = new UITextField()
{
   UserInteractionEnabled = true,
};
if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
{
  _txtField.TextContentType = UITextContentType.OneTimeCode;          
}
_txtFieldDelegate = new UITextFieldDelegate();
_txtField.Delegate = _txtFieldDelegate;
_txtField.BecomeFirstResponder();
Run Code Online (Sandbox Code Playgroud)

注意:安全代码自动填充仅适用于系统键盘(非自定义)。


Ant*_*tDC 7

另外...在电话上需要打开“自动填充密码”。

  • 叹。这是我见过的唯一提到这一点的地方。我一直在调试并想知道为什么代码不起作用...... (2认同)

Ram*_*rma 6

UPDATE

从iOS 12开始,Apple将支持UITextField,UITextView以及采用UITextInput协议的任何自定义视图上的密码自动填充.系统键盘将textContentType设置为.oneTimeCode

1)使用代码

singleFactorCodeTextField.textContentType = .oneTimeCode

2)使用Storyboard/XIB

选择UITextField/ UITextViewstoryboard/XIB单击单击属性检查器.转到文本输入特征,单击内容类型并选择一个时间代码并完成.

操作系统将使用此UITextContentType设置自动检测消息中的验证码.

警告

如果对安全代码输入文本字段使用自定义输入视图,则iOS无法显示必要的自动填充UI.

WWDC 2018 iPhoneX设备

有关更多信息,可以在Apple开发人员oneTimeCode上进行检查

并且还要检查WWDC 2018会话204 - 自动强密码和安全代码自动填充并跳转到24:28以自动预填充OTP.


Bha*_*awa 6

我从santosh kumarTed的回答中得到了解决方案

var otpText = String()
Run Code Online (Sandbox Code Playgroud)
  • viewDidload()
     if #available(iOS 12.0, *) {
         txtFirst.textContentType = .oneTimeCode
         txtSecond.textContentType = .oneTimeCode
         txtThird.textContentType = .oneTimeCode
         txtForth.textContentType = .oneTimeCode
         txtFifth.textContentType = .oneTimeCode
     }

     txtFirst.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
     txtSecond.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
     txtThird.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
     txtForth.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
     txtFifth.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
     txtFirst.becomeFirstResponder() //by doing this it will open the keyboard on first text field automatically

Run Code Online (Sandbox Code Playgroud)
  • 行动TextField
   //When changed value in textField
    @objc func textFieldDidChange(textField: UITextField){
        let text = textField.text
        if  text?.count == 1 {
            switch textField{

            case txtFirst:
                txtSecond.becomeFirstResponder()
            case txtSecond:
                txtThird.becomeFirstResponder()
            case txtThird:
                txtForth.becomeFirstResponder()
            case txtForth:
                txtFifth.becomeFirstResponder()
            case txtFifth:
                txtFifth.resignFirstResponder()
                self.dismissKeyboard()
            default:
                break
            }
        }
        if  text?.count == 0 {
            switch textField{
            case txtFirst:
                txtFirst.becomeFirstResponder()
            case txtSecond:
                txtFirst.becomeFirstResponder()
            case txtThird:
                txtSecond.becomeFirstResponder()
            case txtForth:
                txtThird.becomeFirstResponder()
            case txtFifth:
                txtForth.becomeFirstResponder()
            default:
                break
            }
        }
        else{

        }
    }
Run Code Online (Sandbox Code Playgroud)
  • OTP 字符串和关闭键盘
 func dismissKeyboard(){

        self.otpText = "\(self.txtFirst.text ?? "")\(self.txtSecond.text ?? "")\(self.txtThird.text ?? "")\(self.txtForth.text ?? "")\(self.txtFifth.text ?? "")"

        print(self.otpText)
        self.view.endEditing(true)

    }
Run Code Online (Sandbox Code Playgroud)

最重要的是:如果您正在使用shouldChangeCharactersIn方法,请发表评论。否则这段代码将无法工作


Ted*_*Ted 5

UPDATE

现在可以在iOS 12中使用安全码自动填充功能

自iOS 11以来,Apple正在苹果支付卡验证中使用它

在此输入图像描述


小智 -10

不。

由于这将被视为隐私问题,因此您无法访问用户的 SMS 收件箱。