gpt4 book ai didi

ios - 调用云代码 Parse Swift

转载 作者:搜寻专家 更新时间:2023-11-01 06:41:50 24 4
gpt4 key购买 nike

我正在尝试在我的应用中使用电话号码验证。在注册过程中,用户将被要求提供他们的电话号码并收到验证码,他们将输入该代码,如果匹配则可以继续。我已经准备好云代码,但我不确定如何从应用程序中调用它。任何帮助将不胜感激

我希望能够做的是,当他们输入电话号码并按继续时,首先检查电话号码是否有效,然后发送短信代码。最后转到验证 View ,他们在其中输入代码,如果不匹配则显示警报,如果匹配则让用户登录

云代码:

var twilioAccountSid = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var twilioAuthToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var twilioPhoneNumber = '+1555-555-5555';
var secretPasswordToken = '1234';


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: twilioPhoneNumber,
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.");
}
});

这是我调用云代码的地方:

@IBOutlet weak var continueButton: UIButton!
@IBOutlet weak var phoneNumberTextField: UITextField!
@IBAction func continueButtonTapped(sender: AnyObject) {
var phoneNumber = self.phoneNumberTextField.text

if phoneNumber?.utf16.count < 10 {
displayAlert("Error", message: "You must enter a valid 10 digit US phone number including area code")
} else {
let params = phoneNumber
PFCloud.callFunctionInBackground("sendVerificationCode", withParameters: params, block: { (object: AnyObject?, error) -> Void in
if error == nil {
self.performSegueWithIdentifier("showVerifyUserView", sender: self)
} else {

// Do error handling
}
})
}
}

最佳答案

你应该使用这个方法:

public class func callFunctionInBackground(function: String, withParameters parameters: [NSObject : AnyObject]?, block: PFIdResultBlock?)

你可以这样调用你的函数:

        PFCloud.callFunctionInBackground("sendVerificationCode",
withParameters: /* any needed params here, for example mobile phone*/)
{ [unowned self](object:AnyObject?, error:NSError?) -> Void in
guard error == nil else {
//handle error
return
}
// everything ok
// notify your app that code is send successfully

}

然后当用户输入代码时做同样的事情:

        PFCloud.callFunctionInBackground("verifyPhoneNumber",
withParameters: /* any needed params here, for example verification code*/)
{ [unowned self](object:AnyObject?, error:NSError?) -> Void in
guard error == nil else {
//handle wrong notification code
return
}
// code is ok, notify your app verification is complete
}

不要忘记部署代码以使用命令行实用程序进行解析 + 考虑到将参数发送到云代码的常用方法是发送字典

更新,如何发送参数:

PFCloud.callFunctionInBackground("functionName",
withParameters: ["phoneNumber" : phoneNumber)
{ [unowned self](object:AnyObject?, error:NSError?) -> Void in
...


}

关于ios - 调用云代码 Parse Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34276898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com