2013-10-31 42 views
0

我想從IOS代碼手動在parse.com上註冊一個新用戶。我需要這個,因爲我想在註冊時添加3-4個額外的字段。這裏是我正在嘗試的代碼,但它不是將數據插入用戶類:是否可以在parse.com中註冊新用戶?

PFObject *testObject = [PFObject objectWithClassName:@"User"]; 
    [testObject setObject:@"bar" forKey:@"username"]; 
    [testObject setObject:@"bar" forKey:@"password"]; 
    [testObject setObject:@"1111111" forKey:@"additional"]; 
    [testObject setObject:@"[email protected]" forKey:@"email"]; 
    [testObject setObject:@"2323233" forKey:@"phoneNumber"]; 
    [testObject setObject:@"12,california" forKey:@"address"]; 
    [testObject save]; 

請任何幫助。我在這裏做錯了什麼?

+0

要向PFUser添加更多字段,您必須(1)首先以正常方式創建用戶。 (2)在一個單獨的呼叫中,發送其他字段。這是唯一的方法。 – Fattie

+0

@JoeBlow你不需要把它分成2個請求... – Kasztan

+0

你必須使用兩個調用。首先創建新的PFUser,然後添加額外的字段。 – Fattie

回答

3

注 - 這個答案不使用標準PFUser類語法分析。

本質上它不起作用。如果您沒有使用實際的PFUser類,則Parse中沒有任何工作(例如,電子郵件驗證,密碼重置,帳戶處理,等等)

使用PFUser添加額外字段非常簡單:(1)創建用戶以正常的方式。 (2)在另一個電話,發送其他領域。很多時候你也會有一個圖像(像用戶頭像) - 在另一個cal中單獨發送。


很多RND後,我發現這是一個解決我的問題。我在這裏分享它這樣,這將是對別人也是有用的

-(void)userSignUp:(NSDictionary*)userData { 
    //username,password,additional,email,profilePic 

    ADUser *user = [[ADUser alloc] init]; 
    user.username = [userData valueForKey:@"username"]; 
    user.password = [userData valueForKey:@"password"]; 
    user.email = [userData valueForKey:@"email"]; 
    user.phone = [userData valueForKey:@"phone"]; 
    user.additional = [userData valueForKey:@"name"]; 

    PFUser *usr = (PFUser*)user; 

    [usr signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) 
    { 
     if (error) // Something went wrong 
     { 
      // Display an alert view to show the error message 
      UIAlertView *alertView = 
      [[UIAlertView alloc] initWithTitle:[[error userInfo] objectForKey:@"error"] 
            message:nil 
            delegate:self 
          cancelButtonTitle:nil 
          otherButtonTitles:@"Ok", nil]; 
      [alertView show]; 

      // Bring the keyboard back up, user will probably need to change something 
      //[usernameField becomeFirstResponder]; 
     } else { 
      NSLog(@"success"); 
      [self dismissViewControllerAnimated:YES completion:nil]; 

     } 

    }]; 
} 
+1

您應該使用@LostInTheTrees答案,您沒有工作的原因是因爲您未使用PFUser類。 –

+0

我也嘗試過使用它,但它對我無效。我實際上看到的是signUpInBackgroundWithBlock方法。這是我發現註冊自定義數量的字段的唯一方法。 @LostInTheTrees的答案在某種程度上是正確的,但不符合我的要求,因爲這個問題的標題表明。但我真的很感謝LostInTheTrees的回答。 – stackNeverFlow

+0

註冊成功後,您可以隨時分配自定義字段,如在'signUpInBackgroundWithBlock'代碼塊 –

2

我相信你需要

PFUser *user = [PFUser user]; 
user.username = @"my name"; 
user.password = @"my pass"; 
user.email = @"[email protected]"; 

// other fields can be set just like with PFObject 

-Bob

+0

它不允許以這種方式添加電話號碼。所以首先我們需要繼承PFUser,然後我們可以添加額外的字段。如果我在這裏錯了,請糾正我。 – stackNeverFlow

+0

你錯了。 「其他字段可以像使用PFObject一樣設置」 – Rambatino

2

子類PFUser像這樣

User.h

#import <Parse/Parse.h> 

@interface User : PFUser <PFSubclassing> 

@property (nonatomic, strong) NSString *nickName; 
@property (nonatomic, strong) NSString *username; 
@property (nonatomic, strong) NSString *email; 
@property (nonatomic, strong) NSString *type; 

@property (nonatomic, strong) PFFile* image; 


@end 

User.m

#import "User.h" 

@implementation User 
@dynamic nickName, email, type; 

+(void)load { 
    [self registerSubclass]; 
} 

+ (NSString *)parseClassName { 
    return @"_User"; 
} 

@end 

然後使用以下代碼註冊:

User *user = [PFUser user]; 
user.nickName = @"Alpha"; 
user.username=username; 
[email protected]"0000"; 

[user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) 
{ 
    if (!error) 
    { 
     NSLog(@"Signup Success"); 
    } 
    else{ 
     NSLog(@"Signup Error : %@ ", error); 
    } 
}]; 

這就是我們如何使用PFUser註冊。您始終可以爲PFUser類添加額外的字段。子類提供給你這個特性。

相關問題