2015-12-08 42 views
-1

我正在自定义解析PFLogInViewController并尝试使其登录按钮将触发查询,以查看用户名是否已存在。如果确实存在,则正常登录将继续。如果它不存在,那么用户将被注册。PFLogInViewController - 只注册并登录登录按钮

我最初在查询的异步性质方面有问题,但我将查询从我的函数中分离出来,这似乎不成问题。正如你在代码中看到的,我已经复制了logInViewController函数,所以它的第二个版本有一个额外的参数doesExist: Bool

当我对它进行测试,逻辑上似乎当我在看的打印日志遵循随处可见,但复制的logInViewController功能不实际登录用户英寸

感谢任何意见!

func signUpFunc(username: String, password: String) { 

    //this is the sign up function 

    print(username) 
    print(password) 

    print("this is the sign up function") 

} 

func doesUserExist(username: String, password: String) { 

    var query = PFUser.query() 
    query!.whereKey("username", equalTo: username) 

    query?.findObjectsInBackgroundWithBlock({ (users, error) -> Void in 

     if let users = users { 

      if users.count == 0 { 

       self.signUpFunc(username, password: password) 

      } else { 

       print ("made it here") 

       print(username) 
       print(password) 



       let testController = LoginViewController() 
       self.logInViewController(true, logInController: testController, shouldBeginLogInWithUsername: username, password: password) 



      } 

     } 

    }) 

} 


func logInViewController(logInController: PFLogInViewController, shouldBeginLogInWithUsername username: String, password: String) -> Bool { 

    //this function happens on the initial click of the login button 

    self.doesUserExist(username, password: password) 

    return false 
} 

func logInViewController(doesExist: Bool, logInController: PFLogInViewController, shouldBeginLogInWithUsername username: String, password: String) -> Bool { 

    //this should actually log the user in or give error if user/pass combo is wrong 

    print("made it to login") 
    print(username) 
    print(password) 

    return true 

} 
+0

我实际上没有看到任何地方,你叫'loginWithUsernameInBackground:密码:block' – Paulw11

+0

嗨@ Paulw11我实际上并不在任何地方调用它。当使用PFLogInViewController时,我认为按下登录按钮时内置了一些功能,但我无法在任何地方看到它。我可以在文档中找到的唯一功能是logInViewController(.... shouldBeginLogInWithUsername ....)。我会尝试调用loginWithUsernameInBackground来处理登录端,看看它是否工作。谢谢! –

+0

您正在用您自己的视图控制器替换'PFLoginViewController'的功能,因此只需忽略该类并直接使用'PFUser'上可用的方法 – Paulw11

回答

0

只需尝试使用凭据登录并使用Parse的响应代码。但我不会仅仅创建一个新用户,因为用户不会退出。只是因为用户在登录时出现了输入错误,并且最终会得到新用户,您将获得更多用户。我会先问他们。

func trySignIn() { 
    // get string from textfiled 
    PFUser.logInWithUsernameInBackground("user12", password: "user12") { (user, err) -> Void in 
     if let error = err { 
      if error.code == 101 { 
       let alert = UIAlertController(title: "Error", message: "\(error.localizedDescription) - Should we creat an account for you?", preferredStyle: UIAlertControllerStyle.Alert) 
       let signUpAction = UIAlertAction(title: "Sign up", style: UIAlertActionStyle.Default, handler: { UIAlertAction in 
        self.signUpUser("user12", password: "user12") 
       }) 
       let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) 
       alert.addAction(signUpAction) 
       alert.addAction(cancelAction) 
       self.presentViewController(alert, animated: true, completion: nil) 
      } 
     } else { 
      print("logged in successfully") 
     } 
    } 
} 

func signUpUser(username: String, password: String){ 
    let userToSignUp = PFUser() 
    userToSignUp.username = username 
    userToSignUp.password = password 
    userToSignUp.signUpInBackgroundWithBlock { (success, err) -> Void in 
     if let error = err { 
      self.showAlertViewWithError(error) 
     } else { 
      print("User signed up") // Show message to user 
     } 
    } 
}