Restkit 0.20上的简单JSON登录帖子 [英] Simple JSON login post on Restkit 0.20
问题描述
我遵循这个旧答案的步骤.主要问题是RKObjectLoaderDelegate被删除,即时消息找不到适合我的任何替代方法.我试图用邮件/密码连接到服务器,向json发送发帖请求.此外,我将使用json从数据库中获取数据,然后测试此方法是否正确.有人可以帮我吗?我正在使用RESTkit
Im following the steps of this old answer. The main issue is that RKObjectLoaderDelegate was removed and im cannot find any alternative that works for me. Im trying to connect to a server with mail/password sending a post request to a json. Also, i will be using json to get data from the database after i test this works correct. Someone can help me with this? Im using RESTkit
//Autentificacion
NSURL* url = [[NSURL alloc]initWithString:@"http://myurl.com/tokens.json"];
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:url];
objectManager = [RKObjectManager managerWithBaseURL:url];
就像我之前说过的,我试图做一个JSON登录帖子,但是不同的示例对我不起作用,我正在寻找一些简单的方法在restkit 0.20上做到这一点.我知道如何执行GET而不是POST.
Like i say before, im trying to do a JSON login post but the different examples doesnt work for me, im looking for some simple way to do it on restkit 0.20. I know how to do a GET but not a POST.
推荐答案
首先,我创建了一个名为Login Request的类.
First, i created a class called Login Request.
LoginRequest.h
LoginRequest.h
#import <Foundation/Foundation.h>
#import "RKObjectMapping.h"
@interface LoginRequest : NSObject
@property (nonatomic, strong) NSString* email;
@property (nonatomic, strong) NSString* password;
+(RKObjectMapping*)defineLoginRequestMapping;
@end
LoginRequest.m
LoginRequest.m
#import "LoginRequest.h"
#import "RKObjectManager.h"
@implementation LoginRequest
@synthesize email;
@synthesize password;
+(RKObjectMapping*)defineLoginRequestMapping {
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[LoginRequest class]];
[mapping addAttributeMappingsFromDictionary:@{
@"email": @"email",
@"password": @"password",
}];
return mapping;
}
然后,我制作了一个LoginManager类来进行连接.
Then, i made a LoginManager class to do the connection.
LoginManager.h
LoginManager.h
#import <Foundation/Foundation.h>
#import "RKObjectManager.h"
#import "LoginResponse.h"
#import "LoginRequest.h"
@interface LoginManager : NSObject
@property (nonatomic, strong) LoginRequest *dataObject;
@property (nonatomic, strong) RKObjectManager *objectManager;
@property (nonatomic, strong) AFHTTPClient * client;
-(void)LoginWithUserName:(NSString *)username password:(NSString*)password;
@end
LoginManager.m
LoginManager.m
#import "LoginManager.h"
#import "RKMIMETypeSerialization.h"
#import "RKLog.h"
#import "LoginRequest.h"
@implementation LoginManager
-(void)LoginWithUserName:(NSString *)email password:(NSString*)password {
LoginRequest *dataObject = [[LoginRequest alloc] init];
[dataObject setEmail:email];
[dataObject setPassword:password];
NSURL *baseURL = [NSURL URLWithString:@"http://www.myurl.com/tokens.json"];
AFHTTPClient * client = [AFHTTPClient clientWithBaseURL:baseURL];
[client setDefaultHeader:@"Accept" value:RKMIMETypeJSON];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
RKObjectMapping *requestMapping = [[LoginRequest defineLoginRequestMapping] inverseMapping];
[objectManager addRequestDescriptor: [RKRequestDescriptor
requestDescriptorWithMapping:requestMapping objectClass:[LoginRequest class] rootKeyPath:nil
]];
// what to print
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("Restkit/Network", RKLogLevelDebug);
RKObjectMapping *responseMapping = [LoginResponse defineLoginResponseMapping];
[objectManager addResponseDescriptor:[RKResponseDescriptor
responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:@"" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)
]];
[objectManager setRequestSerializationMIMEType: RKMIMETypeJSON];
[objectManager postObject:dataObject path:@""
parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"It Worked: %@", [mappingResult array]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"It Failed: %@", error);
}];
}
最后,我在MasterViewController.m的viewDidLoad上调用该方法
And finally, i call the method on viewDidLoad on MasterViewController.m
[[LoginManager alloc] LoginWithUserName:@"mymail@test.com" password:@"mypassword"];
这篇关于Restkit 0.20上的简单JSON登录帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!