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