Simple JSON login post on Restkit 0.20

一曲冷凌霜 提交于 2019-12-20 17:32:03

问题


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];

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.


回答1:


First, i created a class called Login Request.

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

#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;

}

Then, i made a LoginManager class to do the connection.

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

#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);

                   }];
}

And finally, i call the method on viewDidLoad on MasterViewController.m

[[LoginManager alloc] LoginWithUserName:@"mymail@test.com" password:@"mypassword"];



回答2:


I guess Restkit has included the AFNetworking 2.0 Library. So I think this would work:

RestKit is a modern Objective-C framework for implementing RESTful web services clients on iOS and Mac OS X. It provides a powerful object mapping engine that seamlessly integrates with Core Data and a simple set of networking primitives for mapping HTTP requests and responses built on top of AFNetworking

Post Example:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
[manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];


来源:https://stackoverflow.com/questions/19632529/simple-json-login-post-on-restkit-0-20

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!