Question

I am new in RestKit. I did not find a proper documentation or tutorial to send simple object data to Restful API.

Here is my problem in detail.

I have a class with name User having two properties for now: email and password. I want to send them to server using RestKit 0.20.

I found some tutorials but all of them are outdated for RestKit v 0.10. I found this question but this is outdated as well. There is no sharedInstance selector of class RKObjectManager in RestKit 0.20 but sharedManager.

Any help would be great.

Était-ce utile?

La solution

Finally I found the solution. Thanks @Mateusz for helping me out. Here is the solution.

// Construct a request mapping for User
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@{ @"email": @"email", @"password": @"password" }];


// construct a response mapping for User
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[User class]];
[responseMapping addAttributeMappingsFromDictionary:@{@"email": @"email", @"password": @"password", @"guid": @"guid"}];


RKRequestDescriptor *req = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[User class] rootKeyPath:@"user" method:RKRequestMethodPOST];

RKResponseDescriptor *res = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"user" statusCodes:[NSIndexSet indexSetWithIndex:200]];

// Register our descriptors with a manager
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost/api/user/"]];

[manager addRequestDescriptor:req];
[manager addResponseDescriptor:res];

// preparing sending User object
User *user = [User new];
user.email = @"example@example.com";
user.password = @"password";

NSLog(@"user email : %@", user.email);

[manager postObject:user path:@"user" parameters:@{@"api_key": MY_API_KEY} success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
    NSArray *arr = [result array];
    User *temp= [arr objectAtIndex:0];
    NSLog(@"SUCCESS ---------------------------- User's email: %@", temp.email);
    NSLog(@"User's guid: %@", temp.guid);

    //                                NSLog(@"--------- - --- -- - all resutl: %@", result);
}
            failure:^(RKObjectRequestOperation *operation, NSError *error) {
                NSLog(@"failed post %@", error);
                NSLog(@"%@",operation.description);
                NSLog(@"%@",operation.HTTPRequestOperation.description);
            }];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top