RESTKit POST请求教程

时间:2013-07-31 00:33:26

标签: ios objective-c restkit restkit-0.20

我想知道是否有关于如何使用RESTKit执行POST请求的特定教程。我看过一些教程,但我没有发现任何说法,“这正是你用RESTKit做POST命令的方式。”非常感谢帮助。

1 个答案:

答案 0 :(得分:11)

假设您已经有一个映射模型,您可以简单地执行此操作:

首先,设置一个requestDescriptor,使用responseDescriptor的inverseMapping,假设你有一个映射。

//This is used for mapping responses, you already should have one of this. PS:[Data mapping] is a method that returns an RKObjectMapping for my model. You should create yours or use a previous created one
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[Data mapping] pathPattern:nil keyPath:@"data" statusCodes:statusCodeSet];
[[RKObjectManager sharedInstance] addResponseDescriptor:responseDescriptor];

//Inverse mapping, to perform a POST
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[[Data mapping] inverseMapping]  objectClass:[Data class] rootKeyPath:nil];
[[RKObjectManager sharedInstance] addRequestDescriptor:requestDescriptor];

之后,要执行POST,只需调用以下方法即可。 Restkit将获取您要发布的实例,将其序列化并发送到所选路径。

[[RKObjectManager sharedInstance] postObject:instanceOfYourModel path:yourPathHere parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"Success");

} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Error");
}];

如果您没有映射模型,请告诉我们,我们可以尝试其他方法。