jsp*_*ner 4 objective-c ios4 restkit
我有用于RestKit的DiscussionBoard应用程序,我正在将DBUser类移植到我的应用程序中.唯一的区别是我的应用程序不会使用CoreData.
我能够POST用户对象并将新用户ID和authentication_token返回给我的应用程序.问题是响应没有被序列化为用户对象.为什么不request:didLoadResponse:response返回User对象,但我可以看到正确的jsonsignUpWithDelegate:delegate
你可以在这里看到我无法objectLoader:didLoadObjects返回序列化对象? 为什么对象没有序列化?
[12881:207]Loaded payload: {"user":{"id":"85","authentication_token":"_Udl98OO-xgmZOOJM26w","email":"ffff@adsfadfa.com"}}
[12881:207] _____ objectLoader didLoadObjects (
)
[12881:207] loginWasSuccessful (null)
Run Code Online (Sandbox Code Playgroud)
AppDelegate.m
RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://spoonbook-2.local:3000/"];
[objectManager.router routeClass:[RKUser class] toResourcePath:@"/api/v1/authentication/create.json" forMethod:RKRequestMethodPOST];
// User
RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[RKUser class]];
[userMapping mapKeyPath:@"id" toAttribute:@"userId"];
[userMapping mapAttributes:@"email", nil];
[userMapping mapAttributes:@"authentication_token", nil];
[objectManager.mappingProvider setObjectMapping:userMapping forKeyPath:@"user"];
Run Code Online (Sandbox Code Playgroud)
RegisterViewController.m
-(IBAction)handleLoginClick:(id)sender
{
NSLog(@"handleLoginClick");
RKUser *user = [[[RKUser alloc] init] retain];
[user createWithEmail:_regEmailTF.text andPassword:_regPasswordTF.text delegate:self];
[user release];
}
Run Code Online (Sandbox Code Playgroud)
User.m
#import "RKUser.h"
#import <RestKit/RestKit.h>
static NSString* const kDBUserCurrentUserIDDefaultsKey = @"kDBUserCurrentUserIDDefaultsKey";
// Current User singleton
static RKUser* currentUser = nil;
@implementation RKUser
@synthesize userID = _userID;
@synthesize email = _email;
@synthesize password = _password;
@synthesize passwordConfirmation = _passwordConfirmation;
@synthesize delegate = _delegate;
@synthesize authentication_token = _authentication_token;
-(void)createWithEmail:(NSString *)username andPassword:(NSString *)password delegate:(NSObject<UserAuthenticationDelegate> *)delegate
{
_delegate = delegate;
RKObjectManager*objectManager = [RKObjectManager sharedManager];
RKObjectLoader* objectLoader = [objectManager objectLoaderWithResourcePath:@"/api/v1/authentication/create.json" delegate:self];
objectLoader.method = RKRequestMethodPOST;
objectLoader.params = [NSDictionary dictionaryWithKeysAndObjects:@"user[email]", username, @"user[password]", password, @"user[password_confirmation]", password, nil];
objectLoader.targetObject = self;
// try adding a userMapping?
RKObjectMapping *userMapping = [objectManager.mappingProvider objectMappingForKeyPath:@"user"];
objectLoader.objectMapping = userMapping;
// TODO: Temporary to work around bug in Rails serialization on the Heroku app
objectLoader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForKeyPath:@"user"];
[objectLoader send];
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response
{
NSLog(@"ReviewFormViewController Loaded payload: %@", [response bodyAsString]);
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray *)objects
{
// NOTE: We don't need objects because self is the target of the mapping operation
NSLog(@"_____ objectLoader didLoadObjects %@", objects);
if ([objectLoader wasSentToResourcePath:@"/api/v1/authentication/login.json"]) {
// Login was successful
[self loginWasSuccessful];
} else if ([objectLoader wasSentToResourcePath:@"/api/v1/authentication/create.json"]) {
// Sign Up was successful
if ([self.delegate respondsToSelector:@selector(userDidSignUp:)]) {
[self.delegate userDidSignUp:self];
}
// Complete the login as well
[self loginWasSuccessful];
}
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError*)error {
...snip...
}
- (void)loginWasSuccessful {
...snip...
}
@end
Run Code Online (Sandbox Code Playgroud)
我认为您需要使用它才能映射响应.
/**
Send the data for a mappable object by performing an HTTP POST. The data returned in the response will be mapped according
to the object mapping provided.
*/
- (RKObjectLoader*)postObject:(id<NSObject>)object mapResponseWith:(RKObjectMapping*)objectMapping delegate:(id<RKObjectLoaderDelegate>)delegate;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2921 次 |
| 最近记录: |