0

我正在尝试从 IOS 代码在 parse.com 上手动注册一个新用户。我需要这个,因为我想在注册时添加 3-4 个额外字段。这是我正在尝试的代码,但它没有将数据插入用户类:

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

请提供任何帮助。我在这里做错了什么?

4

3 回答 3

4

注意 - 此答案在 PARSE 中不使用标准 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];

         }

     }];
}
于 2013-10-31T18:20:38.370 回答
2

我相信你需要

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

// other fields can be set just like with PFObject

-鲍勃

于 2013-10-31T16:22:15.257 回答
2

子类 PFUser 像这样

用户.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

用户.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;
user.password=@"0000";

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

这就是我们使用 PFUser 进行注册的方式。您始终可以向 PFUser 类添加额外的字段。子类化为您提供了此功能。

于 2015-07-30T11:29:27.260 回答