有没有办法从iPhone收件箱(短信)中访问数据,以便像Android中那样进行自动OTP验证的iOS应用程序?感谢您的帮助。
有没有办法从iPhone收件箱(短信)中访问数据,以便像Android中那样进行自动OTP验证的iOS应用程序?感谢您的帮助。
iOS 12中,苹果公司引入了名为“安全码自动填充”的功能。
要在您的应用程序中使用此功能,您所需做的就是设置UITextField
输入视图的textContentType
属性为oneTimeCode
即可。
otpTextField.textContentType = .oneTimeCode
注意:安全代码自动填充只能与系统键盘一起使用,无法与自定义键盘一起使用。
当你收到 OTP 时,它会看起来像这样:
或者“您的密码是:123456”
类似这样的内容。 不要出现以下情况!“12345是您登录的代码”
您可以通过点击短信中下划线标记的代码来验证该代码是否适用于.oneTimeCode类型。如果弹出对话框显示“复制代码”,则表示您可以继续使用。否则,您可能需要更改短信的文本内容。Your App: 12345
更新
从 iOS 12 开始,苹果将在 UITextField、UITextView 和采用 UITextInput 协议的任何自定义视图上支持密码自动填充。系统键盘会将 textContentType 设置为.oneTimeCode。
1)使用代码
singleFactorCodeTextField.textContentType = .oneTimeCode
2)使用 Storyboard/XIB
在
storyboard/XIB
中,选择UITextField
/UITextView
并点击属性检查器。进入文本输入特性,点击内容类型并选择一次性代码即可完成设置。
操作系统会根据设置的 UITextContentType
自动检测来自短信的验证码。
注意
如果您为安全代码输入文本字段使用自定义输入视图,则 iOS 无法显示必要的自动填充 UI。
有关更多信息,请参阅 Apple Developer 网站上的 oneTimeCode .
另外还需查看WWDC 2018 Session 204,跳转至24:28以实现自动填充OTP功能。
目前针对iOS 12及以上版本,您可以使用安全代码自动填充
oneTimeCodeTextField.textContentType =.oneTimeCode
然而,自 iOS 11 以来,ApplePay 已经开始进行自动验证,但这项功能对开发者尚不可用。
此外,在手机上,“自动填充密码”需要打开。
要在文本字段上获取otp,需要注意以下两点:
1. 将您的输入字段设置为otp
其他答案会告诉您如何管理代码以接收otp。
2. 在您的消息中提供“code”短语
您的消息必须包含英语中的“code”,“passcode”,“password”,如果您想将其启用在其他语言中测试一些从“code”翻译到您的语言的单词。
我收到了以下单词的消息:
西班牙语:(Codingo)
德语:(Kode)
捷克语:(Kod)
波斯语:(رمز)
阿拉伯语:(رمز)
otp数字必须是英语而不是您的语言数字,“Code”短语必须与您的数字分开,只有一个空格或使用“:”没有空格。
Code 1111111
Code:111111
另一个重要的事情是关于消息预览。如果数字下面有灰线,则数字将被检测为otp。如果它显示为蓝色或没有线,则消息不会通知您作为otp。像图片一样的东西。
_txtField = new UITextField()
{
UserInteractionEnabled = true,
};
if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
{
_txtField.TextContentType = UITextContentType.OneTimeCode;
}
_txtFieldDelegate = new UITextFieldDelegate();
_txtField.Delegate = _txtFieldDelegate;
_txtField.BecomeFirstResponder();
注意:安全码自动填充仅适用于系统键盘(非定制键盘)。
我从santosh kumar和Ted的答案中得到了解决方案。
var otpText = String()
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
Action
//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{
}
}
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
方法,请加上注释。否则,此代码将无法正常工作。
textContentType
仅设置为.oneTimeCode
,适用于OTP文本框。 - steveSarsawaotptextField.textContentType = .oneTimeCode
请从以下链接获取该项目。