我正在尝试在我的应用中使用电话号码验证。在注册过程中,将询问用户他们的电话号码并发送验证码,他们将输入代码,如果匹配,他们可以继续。我已准备好云代码,但我不确定如何从应用程序中调用此代码。任何帮助将不胜感激
我希望能够做的是当他们输入电话号码并按继续时,首先检查电话号码是否是有效的电话号码,然后发送短信代码。最后转到验证视图,在那里他们输入代码,如果不匹配则显示警告,如果是,则记录用户
云代码:
// Require and initialize the Twilio module with your credentials
var twilio = require("twilio")("twilioAccountSid","twilioAuthToken");
Parse.Cloud.define("sendVerificationCode", function(request, response) {
var verificationCode = Math.floor(Math.random()*999999);
var user = Parse.User.current();
user.set("phoneVerificationCode", verificationCode);
user.save();
twilio.sendSms({
from: '+14123854839',
To: request.params.phoneNumber,
Body: "Your verification code is " + verificationCode + "."
}, function(err, responseData) {
if (err) {
response.error(err);
} else {
response.success("Success");
}
});
});
Parse.Cloud.define("verifyPhoneNumber", function(request, response) {
var user = Parse.User.current();
var verificationCode = user.get("phoneVerificationCode");
if (verificationCode == request.params.phoneVerificationCode) {
user.set("phoneNumber", request.params.phoneNumber);
user.save();
response.success("Success");
} else {
response.error("Invalid verification code.");
}
});
swift代码发送代码:
@IBAction func send(_ sender: Any) {
PFCloud.callFunction(inBackground: "sendVerificationCode", withParameters: ["phoneNumber" : phoneNumber], block: { (results, error) in
if error == nil {
let aVC = self.storyboard?.instantiateViewController(withIdentifier: "VerificationCode") as! VerificationCode
self.present(aVC, animated: true, completion: nil)
print("sent")
} else {
print ("\(error!.localizedDescription)")
}
})
}