2015-09-04 45 views
1

我希望我的應用程序能夠在沒有正確憑證的情況下阻止用戶登錄(與其他應用程序非常相似)。我正在使用Parse並在他們的iOS源代碼文檔中使用他們的代碼作爲登錄序列,但只要我登錄按鈕時沒有任何憑據,甚至是錯誤的登錄按鈕,它仍然適用於sucessfulLoginPage segue標識符,而不先檢查其憑據。我已經嘗試過所有我知道如何去做而沒有取得任何成功的人,有沒有人可以幫助我呢?下面我有我的代碼。解析沒有有效的郵票的登錄過程

PFUser.logInWithUsernameInBackground(username, password:password) { 
    (user: PFUser?, error: NSError?) -> Void in 
    if user != nil { 
     self.performSegueWithIdentifier("sucessfulLoginPage", sender: self) 
    } else if username.isEmpty || password.isEmpty { 
     var emptyFieldsError:UIAlertView = UIAlertView(title: "Please try again", message: "Please fill in all the fields we can get you logged in to your account.", delegate: self, cancelButtonTitle: "Try again") 
     emptyFieldsError.show() 
     self.loginActivity.hidden = true 
     self.loginButton.userInteractionEnabled = true 
     self.loginButton.alpha = 1.0 
     self.loginActivity.stopAnimating() 

     self.usernameField.userInteractionEnabled = true 
     self.passwordField.userInteractionEnabled = true 
     self.forgotPasswordLabel.userInteractionEnabled = true 
    } else { 
     self.loginActivity.hidden = true 
     self.loginButton.userInteractionEnabled = true 
     self.loginButton.alpha = 1.0 
     self.loginActivity.stopAnimating() 

     self.usernameField.userInteractionEnabled = true 
     self.passwordField.userInteractionEnabled = true 
     self.forgotPasswordLabel.userInteractionEnabled = true 

     var invalidLogin:UIAlertView = UIAlertView(title: "Please try again", message: "The username password combo you gave us does not match our records, please reset your password or try again.", delegate: self, cancelButtonTitle: "Try again") 
     invalidLogin.show() 
    } 
} 
+0

也許是因爲'user'不是'nil'? –

回答

1

如果您啓用了自動用戶,可能會發生這種情況。在AppDelegate中搜索PFUser.enableAutomaticUser()並將其刪除或註釋掉。

+0

這是在AppDelegate中,但是當我將它註釋掉時,它仍然會這樣做。 –

+0

@TrentonTyler它仍然會這樣做,因爲設備上可能還有一個緩存的用戶。一旦用戶登錄後,他們會保持登錄狀態,直到您告訴應用程序將其註銷。因此,只需調用'PFUser.logout'或其它任何適用於swift的工具,以確保沒有登錄用戶進行測試。或者只是刪除應用程序並重新安裝 – soulshined