ios iOS中的自动OTP验证?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39631168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 10:19:09  来源:igfitidea点击:

Automatic OTP verification in iOS?

iosiphonesmsone-time-passwordios12

提问by Rinshad Kammath

Is there any way to access data from iPhone inbox(SMS) to ios application to do automatic OTP verification like the one in Android? I shall be grateful for your help.

有什么方法可以从 iPhone 收件箱(SMS)访问数据到 ios 应用程序来进行自动 OTP 验证,就像 Android 中的一样?你的帮助我将不胜感激。

回答by iVarun

In iOS 12 Apple has introduced feature called Security Code AutoFill.

在 iOS 12 中,Apple 引入了名为Security Code AutoFill.

To use this in your app all you need to do is set UITextField's input view's textContentTypeproperty oneTimeCode.

要在您的应用程序中使用它,您需要做的就是设置UITextField输入视图的textContentType属性oneTimeCode

otpTextField.textContentType = .oneTimeCode

NOTE: Security Code AutoFill will only works with System Keyboard it will not work with custom keyboard.

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

WWDC video

世界开发者大会视频

When you get OTP it will look something like this:

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

enter image description here

在此处输入图片说明

回答by Ramkrishna Sharma

UPDATE

更新

From iOS 12 Apple will supports Password AutoFill on UITextField, UITextView, and any custom view that adopts the UITextInput protocol. System keyboard set the textContentType on it to .oneTimeCode

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

1) Using Code

1) 使用代码

singleFactorCodeTextField.textContentType = .oneTimeCode

singleFactorCodeTextField.textContentType = .oneTimeCode

2) Using Storyboard/XIB

2) 使用故事板/XIB

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

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

The operating system will detect verification codes from Messages automatically with this UITextContentTypeset.

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

Warning

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

警告

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

WWDC 2018 iPhoneX Device

WWDC 2018 iPhoneX 设备

For more information, you can check it on the Apple developer oneTimeCode

有关更多信息,您可以在 Apple 开发者oneTimeCode 上查看

And also review WWDC 2018 Session 204 - Automatic Strong Passwords and Security Code AutoFilland jump to 24:28for automatic pre-fill the OTP.

并查看WWDC 2018 Session 204 - Automatic Strong Passwords and Security Code AutoFill并跳转到24:28以自动预填充 OTP。

回答by Ted

Currently for iOS 12 and above, you may use Security Code Autofill

目前对于 iOS 12 及以上,您可以使用安全码自动填充

oneTimeCodeTextField.textContentType =.oneTimeCode

However ApplePay is doing automatic verification since iOS 11 but that is not yet available to developers.

然而,ApplePay 自 iOS 11 起就开始进行自动验证,但尚未对开发人员可用。

回答by El0din

In Xamarin iOS, for >=iOS 12:

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

First of all, the SMS need to have the keyword "code" or "passcode"into their message, and don't use spaces after the code. if you received the SMS and you have the button "Copy Code"then it will works

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

enter image description here

在此处输入图片说明

Then you need to place this:

然后你需要放置这个:

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

NOTE: Security Code AutoFill will only works with System Keyboard (not custom).

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

回答by king_T

you can easily set this in Storyboard

您可以在 Storyboard 中轻松设置

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

单击属性检查器。 转到文本输入特征,单击内容类型并选择一个时间码

回答by iVentis

It is also important that the text message you receive contains something with "code" like

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

"your passcode is:123456"

“您的密码是:123456”

or

或者

"12345 is your code to log in"

“12345是你的登录密码”

something along that line.

沿着这条线的东西。

NOT!

不是!

Your App: 12345

您的应用:12345

you can verify if the code in your text message will work with the .oneTimeCode type by tapping the underlined code in your message. If a dialog pops up that says "copy code", you are good to go. Otherwise you might need to change the text of your message.

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

回答by AntDC

Also...on the phone "Autofill Passwords" needs to be turned on.

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

回答by Karthickkck

You can get OTP from your message.

您可以从您的消息中获取 OTP。

otptextField.textContentType = .oneTimeCode

Can please get the project from his link.

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

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

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

回答by steveSarsawa

I've got sollution from answer of santosh kumarand Ted

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

var otpText = String()
  • in viewDidload()
  • 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)
  • Actionfor TextField
  • 行动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{

        }
    }
  • OTP String and Dismiss KeyBoard
  • 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)

    }

回答by Mika

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

when connected make a lambda ( e-> ""); !!

连接时制作一个 lambda ( e-> ""); !!