调用云代码Parse Swift

时间:2015-12-14 21:25:47

标签: ios swift parse-platform cloud-code

我正在尝试在我的应用中使用电话号码验证。在注册过程中,将询问用户他们的电话号码并发送验证码,他们将输入代码,如果匹配,他们可以继续。我已准备好云代码,但我不确定如何从应用程序中调用此代码。任何帮助将不胜感激

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

云代码:

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
            }
        })
    }
}

2 个答案:

答案 0 :(得分:3)

您应该使用此方法:

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
                ...


        }

答案 1 :(得分:0)

假设您已将代码部署到Parse(命令行“parse deploy”),则使用PFCloud.callFunctionInBackground()调用云函数。见https://www.parse.com/docs/ios/api/Classes/PFCloud.html