当新用户使用Swift和Parse登录时,数据不会更新到当前用户

时间:2015-12-08 00:49:54

标签: ios swift parse-platform

我正在开发一个使用电话号码和短信验证登录的应用。除了一个小问题外,一切运作良好。如果我注销一个用户,然后使用另一个用户登录,则会加载先前的用户数据,但会创建一个新用户,但会显示之前的用户数据。我必须注销并再次登录新用户才能加载他们的数据。有人看到有什么事吗?

登录代码:

class LoginViewController: UIViewController {

func displayAlert(title: String, message: String) {
    var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
    }))
    self.presentViewController(alert, animated: true, completion: nil)
}


@IBOutlet weak var instructionLabel: UILabel!
@IBOutlet weak var phoneNumberTextField: UITextField!
@IBOutlet weak var sendCodeButton: UIButton!

var phoneNumber: String = ""



override func viewDidLoad() {
    super.viewDidLoad()

    first()
    self.editing = true
}

func first() {
    phoneNumber = ""
    phoneNumberTextField.placeholder = "555-555-5555"
    instructionLabel.text = "Enter your phone number to login or sign up"
    sendCodeButton.enabled = true
}

func second() {

    phoneNumber = phoneNumberTextField.text!
    phoneNumberTextField.text = ""
    phoneNumberTextField.placeholder = "1234"
    instructionLabel.text = "Enter your 4 digit security code"
    sendCodeButton.enabled = true
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    phoneNumberTextField.becomeFirstResponder()
}

@IBAction func didTapSendCodeButton() {

    let preferredLanguage = NSBundle.mainBundle().preferredLocalizations[0]

    let textFieldText = phoneNumberTextField.text ?? ""

    if phoneNumber == "" {
        if (preferredLanguage == "en" && textFieldText.characters.count != 10) {
            displayAlert("Phone Login", message: NSLocalizedString("warningphone", comment: "You must enter a 10 digit US phone number including area code"))
            return first()
        }

        self.editing = false
        let params = ["phoneNumber" : textFieldText, "language" : preferredLanguage]
        PFCloud.callFunctionInBackground("sendCode", withParameters: params) { response, error in
            self.editing = true
            if let error = error {
                var description = error.description
                if description.characters.count == 0 {
                    description = NSLocalizedString("warningGeneral", comment: "Something went Wrong. Please try again")
                } else if let message = error.userInfo["error"] as? String {
                    description = message
                }
                self.displayAlert("Login Error", message: description)
                return self.first()
            }
            return self.second()
        }
    } else {
        if textFieldText.characters.count == 4, let code = Int(textFieldText) {
            return doLogin(phoneNumber, code: code)
        }

        displayAlert("Code Entry", message: NSLocalizedString("warningCodeLength", comment: "You must enter the 4 digit code texted to your number"))
    }
}

func doLogin(phoneNumber: String, code: Int) {

    self.editing = false
    let params = ["phoneNumber": phoneNumber, "codeEntry": code] as [NSObject:AnyObject]
    PFCloud.callFunctionInBackground("logIn", withParameters: params) { response, error in
        if let description = error?.description {
            self.editing = true
            return self.displayAlert("Login Error", message: description)
        }
        if let token = response as? String {
            PFUser.becomeInBackground(token) { user, error in
                if let _ = error{
                    self.displayAlert("Login Error", message: NSLocalizedString("warningGeneral", comment: "Something happened while logging in. Please try again"))
                    self.editing = true
                    return self.first()
                }

                return self.dismissViewControllerAnimated(true, completion: nil)
            }
        } else {
            self.editing = true
            self.displayAlert("Login Error", message: NSLocalizedString("warningGeneral", comment: "Something went wrong. Please try again"))
            return self.first()
        }
    }
}

override func setEditing(editing: Bool, animated: Bool) {
    sendCodeButton.enabled = editing
    phoneNumberTextField.enabled = editing
    if editing {
        phoneNumberTextField.becomeFirstResponder()
    }
}
}

extension LoginViewController : UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
    self.didTapSendCodeButton()

    return true

}
}

1 个答案:

答案 0 :(得分:0)

发现我必须在viewWillAppear

中更新标签的问题