2015-08-09 80 views
0

我沒有收到任何錯誤,應用程序生成良好,但是當我檢查解析時,沒有添加新用戶。這裏是代碼:iOS Swift +解析用戶註冊不傳遞任何數據來解析

import UIKit 
import Parse 

class ViewController: UIViewController { 

    @IBOutlet var passwordTextField: UITextField! 

    @IBOutlet var emailTextField: UITextField! 

    //@IBOutlet var messageLabel: UILabel! 


    @IBAction func loginVerifyButton(sender: AnyObject) { 

     var pwdEntered = passwordTextField.text 

     var emlEntered = emailTextField.text 

     if pwdEntered != "" && emlEntered != "" { 

      // If not empty then yay, do something 

      func userSignUp() { 

       var user = PFUser() 
       user.email = emlEntered 
       user.password = pwdEntered 
       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 { 

      //self.messageLabel.text = "All Fields Required" 

     } 
+0

你嘗試添加用戶名PFUser所以,從這個改變? – ridvankucuk

+0

如果您在「if let error = error」行設置了Xcode斷點,那麼當您按下loginVerify按鈕時該命令會被觸發嗎?此外,'signUpInBackgroundWithBlock'用於創建新帳戶,但您的函數名稱爲'loginVerifyButton'。 [有一個單獨的PFUser登錄方法](https://parse.com/docs/osx/api/Classes/PFUser.html#//api/name/logInWithUsername:password:error :),你究竟在嘗試去做? –

+0

這是在iOS9上嗎? App Transport Security在最近的幾個beta中啓用,因此您需要在info.plist中添加解析異常。這是一篇很好的文章,可以在運行iOS 9時進行設置:http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/ –

回答

0

你應該設置PFUser者的用戶名,以及,你只設置電子郵件和passwrod,但你應該這樣做,ObjC,抱歉:

PFUser *user = [PFUser user]; 
    user.password = [[[self contentView] passwordField] text]; 
    user.email = [[[self contentView] emailField] text]; 
    user.username = [[[self contentView] emailField] text]; 

然後這樣做:

[user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
    if (succeeded) { 
     //do work 
    } else { 
     //bad stuff 
    } 
}]; 

將用戶名剛剛成立的電子郵件地址 ,如果你閱讀解析API的頭文件,你會發現這個:

@abstract Signs up the user *asynchronously*. 

@discussion This will also enforce that the username isn't already taken. 

@warning Make sure that password and username are set before calling this method. 

@param block The block to execute. 
It should have the following argument signature: `^(BOOL succeeded, NSError *error)`. 
*/ 
- (void)signUpInBackgroundWithBlock:(PF_NULLABLE PFBooleanResultBlock)block; 

這意味着您必須在PFUser中聲明一個用戶名。

var user = PFUser() 
user.email = emlEntered 
user.password = pwdEntered 
user.signUpInBackgroundWithBlock { 

這樣::

func myMethod() { 
    var user = PFUser() 
    user.username = "myUsername" 
    user.password = "myPassword" 
    user.email = "[email protected]" 
    // 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. 
    } 
    } 
} 

從解析的網站本身

+0

任何方式,我可以得到這個快速代碼?我需要有用戶名嗎?我打算只註冊並使用電子郵件和密碼登錄。 – omar3550

+0

呃,是的,直到早上給我,我已經有了大量的編碼,這是在6個小時內,我會爲你張貼一些東西,其實這裏是你的一個片段,重讀我的ansawer,這應該得到你開始 – Loxx

+0

我對ios開發很新,我發現swift代碼更友好....對不起:)感謝您的幫助Larcerax – omar3550