我使用以下代码通过Swift在Parse帐户中注册。
<?php
require_once("a-wons.class.php");
require_once("b-wns.class.php");
// create an instance of class B with own namespace defined
$b = new foo\B();
?>
如果输入的用户名已经保存在Parse中,我想给出错误,但现在它已成功注册了2个相同的用户名。
我该如何避免?
答案 0 :(得分:1)
如果您想通过Parse.com检查用户是否已经注册,那么您必须在用户表中进行查询如果用户已经存在,则不允许他再次注册
PFQuery *query = [PFUser query];
[query whereKey:@"username" equalTo:textField.text];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (object != nil) {
NSLog(@"User exist");
}
else
{
NSLog(@"User don`t exist");
}
}];
希望这会对你有所帮助
答案 1 :(得分:1)
尝试这个
var query = PFQuery(className:"User")
query.whereKey(key: "username", equalTo:self.userNameText.text)
query.getFirstObjectInBackgroundWithBlock { (object: [AnyObject]?, error: NSError?) -> Void in
if object != nil
{
//User exist
//give appropriate message
}
else
{
//User don`t exist"
//call signup method from here
self.signUpNewUser()
}
}
新用户注册
func signUpNewUser()
{
var user = PFUser()
user.username = self.userNameText.text
user.password = self.paswordText.text
user["mobileNumber"] = self.mobileNo as! String //
user["authCode"] = self.authCode as! String
user["deviceId"] = deviceId
self.user.signUpInBackgroundWithBlock({
(success:Bool, error:NSError?) -> Void in
if (error == nil) {
let installation:PFInstallation = PFInstallation.currentInstallation()
installation["user"] = PFUser.currentUser()
installation.saveInBackgroundWithBlock({
(success:Bool, error:NSError?) -> Void in
if (error == nil) {
println("Installion Success")
} else {
println("Installation Error")
}
})
println("Singup Success")
} else {
println(" error = \(error)")
}
})
}