关于iPhone:iOS中的自动OTP验证?

Automatic OTP verification in iOS?

有没有办法像iPhone那样从iPhone收件箱(SMS)到ios应用程序访问数据以进行自动OTP验证? 感谢您的帮助。


苹果在iOS 12中引入了名为Security Code AutoFill的功能。

要在您的应用中使用此功能,只需设置UITextField的输入视图的textContentType属性oneTimeCode

1
otpTextField.textContentType = .oneTimeCode

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

WWDC视频

当您获得OTP时,它将看起来像这样:

enter image description here


更新

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

1)使用代码

singleFactorCodeTextField.textContentType = .oneTimeCode

2)使用情节提要/ XIB

Select UITextField/UITextView in storyboard/XIB click Click on Attribute
inspector. Go to text input trait, click to Content type and select
one time code and done.

设置了此UITextContentType的操作系统将自动从消息中检测验证码。

Warning

If you use a custom input view for a security code input text field,
iOS cannot display the necessary AutoFill UI.

WWDC 2018 iPhoneX Device

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

还可以查看WWDC 2018 Session 204-自动强密码和安全代码自动填充,并跳到24:28以自动预填充OTP。


同样重要的是,您收到的短信中包含带有"代码"的内容,例如

"your passcode is:123456"

要么

"12345 is your code to log in"

沿着这条线。

不!

Your App: 12345

您可以通过点按消息中带下划线的代码来验证短信中的代码是否适用于.oneTimeCode类型。如果弹出一个对话框,提示"复制代码",那就很好了。否则,您可能需要更改消息的文本。


当前适用于iOS 12及更高版本,您可以使用安全代码自动填充

1
oneTimeCodeTextField.textContentType =.oneTimeCode

但是,ApplePay自iOS 11开始就进行自动验证,但开发人员尚未使用。


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


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

首先,SMS的消息中必须包含关键字" code"或" passcode",并且不要在代码后使用空格。如果您收到短信,并且有"复制代码"按钮,那么它将起作用

enter image description here

然后,您需要放置以下内容:

1
2
3
4
5
6
7
8
9
10
11
_txtField = new UITextField()
{
   UserInteractionEnabled = true,
};
if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
{
  _txtField.TextContentType = UITextContentType.OneTimeCode;          
}
_txtFieldDelegate = new UITextFieldDelegate();
_txtField.Delegate = _txtFieldDelegate;
_txtField.BecomeFirstResponder();

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


您可以在情节提要中轻松设置

Click on Attribute inspector. Go to text input trait, click on Content type and select one time code


我从桑托什·库马尔和泰德的答案中得到了解决

1
var otpText = String()
  • viewDidload()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
     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
  • TextField的操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
   //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{

        }
    }
  • OTP字符串和关闭键盘
1
2
3
4
5
6
7
8
 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)

    }

最重要的事情:如果您正在使用shouldChangeCharactersIn方法,请对其进行注释。否则此代码将不起作用


您可以从消息中获得OTP。

1
otpTextField.textContentType = .oneTimeCode

可以从他的链接中获取项目。

https://github.com/karthickkck315/Automatic-OTP


1
2
3
if(server.google==server.connected)}{
   return server;
}

连接时创建lambda ( e->"")
!!


没有。

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