问题
In my app while register i need to send otp to registered number but i am unable to send otp to register number. for registration i have two apis one is register and otpverify... when I click registerButton then i need to send otp to registered mobile num...here how to sen otp to registered mobile number?
Here is my total code:
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!
var otpField: Int?
//MARK:- lifecycle methods
override func viewDidLoad() {
super.viewDidLoad()
self.phoneNumTextField.keyboardType = .phonePad
otpTextField.isHidden = true
}
//MARK:- ButtonActions
@IBAction func registerButton(_ sender: Any) {
registerService()
}
@IBAction func sendOTPButton(_ sender: Any) {
otpService()
}
//MARK:- Service part
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://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]
let phNum = json["mobile_number"] as? Int
self.otpField = json["otp"] as? Int
DispatchQueue.main.async {
self.otpTextField.text = self.otpField as? String
}
}catch{
print("error")
}
}
}).resume()
}
func otpService(){
let parameters = ["mobile_number": phoneNumTextField.text as Any,
"otp": otpTextField.text as Any]
let url = URL(string: "https://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]
let mobileNum = json["mobile_number"] as! [String : Any]
let Uid = mobileNum["id"] as? String
let name = mobileNum["name"] as? String
let phNum = mobileNum["username"] as? String
DispatchQueue.main.async {
if (self.otpTextField.text == String(self.otpField!)){
print("registration successfullllll...")
let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
self.present(loginVC, animated: true)
}
else{
print("register fail")
}
}
}catch{
print("error")
}
}
}).resume()
}
}
Here how to send otp to registerd mobile number. please help me here.
回答1:
There's one thing you should notice:
- You repeated your code: Calling web service. You should have a class in charge of this, you just pass the method (post, get ...) and the parameters.
Regarding to sending OTP to registered phone number, it should be the responsibility of your backend.
And also, it's not the value of otpTextField like this code
let parameters = ["mobile_number": phoneNumTextField.text as Any,
"otp": otpTextField.text as Any]
There're some third parties who can help on phone verification (include sending otp of course) like AccountKit of Facebook (but unfortunately, it stopped since Sep 2019) or Twilio (seems like the best choice at this time).
You can follow this to integrate Twilio phone verification into your application: https://www.twilio.com/blog/phone-verification-in-ios-with-twilio-verify-and-swift-html
回答2:
Well, this is purely backend's responsibility to do but if you still want to try you need to look for some third party api , like twillio sms/otp services and that too if they provide http REST services APIs so that you can hit api from front end directly.
来源:https://stackoverflow.com/questions/58345607/how-to-send-otp-to-registered-mobile-number-in-swift