我为一个很长的问题道歉。大多数代码都是多余的。请看看!!!
首先,我认为指定的信息是在其他部分发送的,似乎是由Parse文档set_error_handler()
指示的:
func myMethod() {
var user = PFUser()
user.username = "myUsername"
user.password = "myPassword"
user.email = "email@example.com"
// other fields can be set just like with PFObject
user["phone"] = "415-392-0202"
user.signUpInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
if let error = error {
let errorString = error.userInfo?["error"] as? NSString
// Show the errorString somewhere and let the user try again.
} else {
// Hooray! Let them use the app now.
}
}
}
请注意,“万岁!让他们现在使用该应用程序。”所以我认为只要控制流程没有达到else{}
,指定的信息就不会被发送到云端。但是我错了。经过多次反复试验,这是我到达的结论。如果signUpInBackgroundWithBlock
等于error
,则nil
会发送必要的信息。如果我错了,请纠正我。这就是原因。只要username
成员是唯一的并且采用“something@something.com”的形式,signUpInBackgroundWithBlock
就会将指定的信息发送到云端,即使,其他字段都是空的。
以下代码非常多余,我为此道歉。只需忽略所有alertController
代码。
import UIKit
import Parse
class SignUpViewController: UIViewController {
@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var lastName: UITextField!
@IBOutlet weak var emailAddress: UITextField!
@IBOutlet weak var password: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
firstName.becomeFirstResponder()
}
@IBAction func signUp(sender: AnyObject) {
let user = PFUser()
user["firstName"] = firstName.text
user["lastName"] = lastName.text
user.email = emailAddress.text
user.password = password.text
user.username = emailAddress.text
user.signUpInBackgroundWithBlock { (succeeded: Bool, error: NSError?) -> Void in
if self.firstName.text == "" {
let alertController = UIAlertController(title: "", message: "please enter your first name", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
self.presentViewController(alertController, animated: true, completion: nil)
} else if self.lastName.text == "" {
let alertController = UIAlertController(title: "", message: "please enter your last name", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
self.presentViewController(alertController, animated: true, completion: nil)
} else if self.emailAddress.text == "" {
let alertController = UIAlertController(title: "", message: "please enter your email", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
self.presentViewController(alertController, animated: true, completion: nil)
} else if self.password.text == "" {
let alertController = UIAlertController(title: "", message: "please enter a password", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
self.presentViewController(alertController, animated: true, completion: nil)
} else if let error = error {
var errorString = error.userInfo?["error"] as? NSString
errorString = String(errorString!)
if (errorString?.rangeOfString("missing username").toRange() != nil) {
errorString = "missing email address"
}
if (errorString?.rangeOfString("username \(self.emailAddress.text) already taken").toRange() != nil) {
errorString = "email \(self.emailAddress.text) already taken"
}
let alertController = UIAlertController(title: "", message: "\(errorString!)", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
self.presentViewController(alertController, animated: true, completion: nil)
} else {
println("SDFJPOIWEJRPOIJWEPOITPOEWIJRPOWIEJRPIWOJEPTIHWEPROIJWPEROIJ")
}
}
}
}
另外,有趣的是,如果error
成员为空,Parse应该生成password
,因为它是PFUser()
的一部分,因为当error
生成时{} username
是空的。但情况似乎并非如此。
所以底线是:如果其他字段为空,我不希望signUpInBackgroundWithBlock
将指定的信息发送到云端。我不知道该怎么做。有人建议解决这个问题吗?
答案 0 :(得分:0)
您只需将验证码移到User.signUpInBackgroundWithBlock
回调:
@IBAction func signUp(sender: AnyObject) {
let user = PFUser()
user["firstName"] = firstName.text
user["lastName"] = lastName.text
user.email = emailAddress.text
user.password = password.text
user.username = emailAddress.text
if firstName.text == "" {
let alertController = UIAlertController(title: "", message: "please enter your first name", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
presentViewController(alertController, animated: true, completion: nil)
} else if lastName.text == "" {
let alertController = UIAlertController(title: "", message: "please enter your last name", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
presentViewController(alertController, animated: true, completion: nil)
} else if emailAddress.text == "" {
let alertController = UIAlertController(title: "", message: "please enter your email", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
presentViewController(alertController, animated: true, completion: nil)
} else if password.text == "" {
let alertController = UIAlertController(title: "", message: "please enter a password", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
presentViewController(alertController, animated: true, completion: nil)
} else {
user.signUpInBackgroundWithBlock { (succeeded: Bool, error: NSError?) -> Void in
if let error = error {
var errorString = error.userInfo?["error"] as? NSString
errorString = String(errorString!)
if (errorString?.rangeOfString("missing username").toRange() != nil) {
errorString = "missing email address"
}
if (errorString?.rangeOfString("username \(self.emailAddress.text) already taken").toRange() != nil) {
errorString = "email \(self.emailAddress.text) already taken"
}
let alertController = UIAlertController(title: "", message: "\(errorString!)", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
//println("Handle Ok logic here")
}))
self.presentViewController(alertController, animated: true, completion: nil)
} else {
println("SDFJPOIWEJRPOIJWEPOITPOEWIJRPOWIEJRPIWOJEPTIHWEPROIJWPEROIJ")
}
}
}
}