如何在 swift 中给重新发送 OTP 按钮一些时间(60 秒)
How to give some time(60 sec) for resend OTP Button in swift
我有带有 OTP 验证的注册屏幕。一旦我点击 regstrButton,OTP 将发送到注册的手机号码。那时如果我点击重新发送 otp 按钮,重新发送 otp 按钮将在 60 秒后显示时间 60 秒我需要重新发送 otp 到注册号码.. 如下图
重新发送按钮需要显示 60 秒后我需要重新发送 otp 如何?
我的 otp 服务代码:
import UIKit
class RegistrationViewController: UIViewController, UITextFieldDelegate
{
//MARK:- Outlets
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var phoneNumTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var conformPasswordTextField: UITextField!
@IBOutlet weak var otpTextField: UITextField!
@IBOutlet weak var registerButton: UIButton!
@IBOutlet weak var sendOtpButton: UIButton!
@IBOutlet weak var otpcountLabel: UILabel!
@IBOutlet weak var resendButn: UIButton!
var otpField: Int?
var otpTimer = Timer()
var totalTime = 10
override func viewDidLoad() {
super.viewDidLoad()
self.phoneNumTextField.keyboardType = .phonePad
otpTextField.isHidden = true
resendButn.isHidden = true
}
@IBAction func registerButton(_ sender: Any) {
if (nameTextField.text == "" || phoneNumTextField.text == "" || passwordTextField.text == "" || conformPasswordTextField.text == "")
{
registerButton.isHidden = false
sendOtpButton.isHidden = true
AlertFun.ShowAlert(title: "Title", message: "RequiredAllFields", in: self)
}
else{
registerButton.isHidden = true
sendOtpButton.isHidden = false
otpTextField.isHidden = false
resendButn.isHidden = false
DispatchQueue.main.async {
self.otpTextField.text = self.otpField as? String
}
registerService()
otpTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(update), userInfo: nil, repeats: true)
}
}
@IBAction func sendOTPButton(_ sender: Any) {
otpService()
}
@IBAction func resendOtpButn(_ sender: Any) {
print("resendotp tapped")
otpTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(update), userInfo: nil, repeats: true)
registerService()
}
@objc func update() {
if(totalTime > 0) {
totalTime = totalTime - 1
print(totalTime)
otpcountLabel.text = String(totalTime)
//resendButn.setTitle("\(totalTime) Resend Otp", for: .normal)
}
else {
//otpcountLabel.isHidden = true
otpTimer.invalidate()
print("call your api")
//registerService()
}
}
//MARK:- Service part
@objc func registerService(){
print("register tapped")
let parameters = ["mobile_number": Int(phoneNumTextField.text ?? "") as Any,
"email":emailTextField.text as Any,
"password":passwordTextField.text as Any,
"name": nameTextField.text as Any]
let url = URL(string: "https://dev.com/webservices/register")
var req = URLRequest(url: url!)
req.httpMethod = "POST"
req.addValue("application/json", forHTTPHeaderField: "Contet-Type")
req.addValue("application/json", forHTTPHeaderField: "Accept")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) else {return}
req.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: req, completionHandler: {(data, response, error) in
if response != nil {
// print(response)
}
if let data = data {
do{
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: Any]
print("the json regggggggggggis \(json)")
self.otpField = json["otp"] as? Int
}catch{
print("error")
}
}
}).resume()
}
@objc func otpService(){
let parameters = ["mobile_number": phoneNumTextField.text as Any,
"otp": otpTextField.text as Any]
let url = URL(string: "https://dev.com/webservices//otpverify")
var req = URLRequest(url: url!)
req.httpMethod = "POST"
req.addValue("application/json", forHTTPHeaderField: "Contet-Type")
req.addValue("application/json", forHTTPHeaderField: "Accept")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) else {return}
req.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: req, completionHandler: {(data, response, error) in
if response != nil {
// print(response)
}
if let data = data {
do{
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: Any]
print("the json of otppppppppp \(json)")
DispatchQueue.main.async {
if (self.otpTextField.text == String(self.otpField!)){
print("registration successfullllll...")
let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.present(loginVC, animated: true)
}
else if self.otpTextField.text == ""{
AlertFun.ShowAlert(title: "", message: "Please enter OTP", in: self)
print("register fail")
}
else {
AlertFun.ShowAlert(title: "", message: "Invalid OTP", in: self)
print("register fail")
}
}
}catch{
print("error")
}
}
}).resume()
}
}
请帮我写代码。
var count = 60 // 60sec if you want
var resendTimer = Timer()
在您的提交按钮上
@IBAction func sendOTPButton(_ sender: Any) {
resendTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(update), userInfo: nil, repeats: true)
}
然后定时器功能
@objc func update() {
if(count > 0) {
count = count - 1
print(count)
btn.setTitle("\(count) Resend Otp", for: .normal)
}
else {
resendTimer.invalidate()
print("call your api")
// if you want to reset the time make count = 60 and resendTime.fire()
}
}
我有带有 OTP 验证的注册屏幕。一旦我点击 regstrButton,OTP 将发送到注册的手机号码。那时如果我点击重新发送 otp 按钮,重新发送 otp 按钮将在 60 秒后显示时间 60 秒我需要重新发送 otp 到注册号码.. 如下图
重新发送按钮需要显示 60 秒后我需要重新发送 otp 如何?
我的 otp 服务代码:
import UIKit
class RegistrationViewController: UIViewController, UITextFieldDelegate
{
//MARK:- Outlets
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var phoneNumTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var conformPasswordTextField: UITextField!
@IBOutlet weak var otpTextField: UITextField!
@IBOutlet weak var registerButton: UIButton!
@IBOutlet weak var sendOtpButton: UIButton!
@IBOutlet weak var otpcountLabel: UILabel!
@IBOutlet weak var resendButn: UIButton!
var otpField: Int?
var otpTimer = Timer()
var totalTime = 10
override func viewDidLoad() {
super.viewDidLoad()
self.phoneNumTextField.keyboardType = .phonePad
otpTextField.isHidden = true
resendButn.isHidden = true
}
@IBAction func registerButton(_ sender: Any) {
if (nameTextField.text == "" || phoneNumTextField.text == "" || passwordTextField.text == "" || conformPasswordTextField.text == "")
{
registerButton.isHidden = false
sendOtpButton.isHidden = true
AlertFun.ShowAlert(title: "Title", message: "RequiredAllFields", in: self)
}
else{
registerButton.isHidden = true
sendOtpButton.isHidden = false
otpTextField.isHidden = false
resendButn.isHidden = false
DispatchQueue.main.async {
self.otpTextField.text = self.otpField as? String
}
registerService()
otpTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(update), userInfo: nil, repeats: true)
}
}
@IBAction func sendOTPButton(_ sender: Any) {
otpService()
}
@IBAction func resendOtpButn(_ sender: Any) {
print("resendotp tapped")
otpTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(update), userInfo: nil, repeats: true)
registerService()
}
@objc func update() {
if(totalTime > 0) {
totalTime = totalTime - 1
print(totalTime)
otpcountLabel.text = String(totalTime)
//resendButn.setTitle("\(totalTime) Resend Otp", for: .normal)
}
else {
//otpcountLabel.isHidden = true
otpTimer.invalidate()
print("call your api")
//registerService()
}
}
//MARK:- Service part
@objc func registerService(){
print("register tapped")
let parameters = ["mobile_number": Int(phoneNumTextField.text ?? "") as Any,
"email":emailTextField.text as Any,
"password":passwordTextField.text as Any,
"name": nameTextField.text as Any]
let url = URL(string: "https://dev.com/webservices/register")
var req = URLRequest(url: url!)
req.httpMethod = "POST"
req.addValue("application/json", forHTTPHeaderField: "Contet-Type")
req.addValue("application/json", forHTTPHeaderField: "Accept")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) else {return}
req.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: req, completionHandler: {(data, response, error) in
if response != nil {
// print(response)
}
if let data = data {
do{
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: Any]
print("the json regggggggggggis \(json)")
self.otpField = json["otp"] as? Int
}catch{
print("error")
}
}
}).resume()
}
@objc func otpService(){
let parameters = ["mobile_number": phoneNumTextField.text as Any,
"otp": otpTextField.text as Any]
let url = URL(string: "https://dev.com/webservices//otpverify")
var req = URLRequest(url: url!)
req.httpMethod = "POST"
req.addValue("application/json", forHTTPHeaderField: "Contet-Type")
req.addValue("application/json", forHTTPHeaderField: "Accept")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) else {return}
req.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: req, completionHandler: {(data, response, error) in
if response != nil {
// print(response)
}
if let data = data {
do{
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: Any]
print("the json of otppppppppp \(json)")
DispatchQueue.main.async {
if (self.otpTextField.text == String(self.otpField!)){
print("registration successfullllll...")
let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.present(loginVC, animated: true)
}
else if self.otpTextField.text == ""{
AlertFun.ShowAlert(title: "", message: "Please enter OTP", in: self)
print("register fail")
}
else {
AlertFun.ShowAlert(title: "", message: "Invalid OTP", in: self)
print("register fail")
}
}
}catch{
print("error")
}
}
}).resume()
}
}
请帮我写代码。
var count = 60 // 60sec if you want
var resendTimer = Timer()
在您的提交按钮上
@IBAction func sendOTPButton(_ sender: Any) {
resendTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(update), userInfo: nil, repeats: true)
}
然后定时器功能
@objc func update() {
if(count > 0) {
count = count - 1
print(count)
btn.setTitle("\(count) Resend Otp", for: .normal)
}
else {
resendTimer.invalidate()
print("call your api")
// if you want to reset the time make count = 60 and resendTime.fire()
}
}