3
votes

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.

2
Please give more details of the mail/password. Are these specified as headers / JSON parameters / what ? - Wain
Im sending that as a JSON parameters to test that the app is connecting to the server. - MidouCloud
Ok, so what is the problem? Show your current code and give details of the response, what you expected but didn't get and errors seen. - Wain
Like i say, i was trying to do the same of this link but RKObjectDelagate was removed and propierties like RKClient doesnt work. So im trying to do the login post in a way similiar to that example. - MidouCloud
Try finding a guide for the current version of RestKit, not the old version. Look specifically at how blocks are used with RKObjectManager. - Wain

2 Answers

11
votes

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:@"[email protected]" password:@"mypassword"];
-1
votes

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