IOS RESTKIT HTTP PUT示例 [英] IOS RESTKIT HTTP PUT example

查看:144
本文介绍了IOS RESTKIT HTTP PUT示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新在REST API中运行的服务器中的数据。我正在使用ios设备上的RESTKIT。但是我找不到如何在restkit中使用PUT。

I want to update data in server which runs in REST API. i am using RESTKIT from ios device. But i could not find how to use PUT in restkit.

我必须发送关键数据:user_id值:2这些格式。任何人都可以帮我解决这个问题.. :(

I have to send data like key:"user_id" value:"2" these format. Can anyone please help me to solve this problem.. :(

推荐答案

SOKeyValue.h:用作通话参数的序列化对象。

SOKeyValue.h : serialized object used as parameter for your call.

#import <Foundation/Foundation.h>

@interface SOKeyValue : NSObject
@property (nonatomic, retain) NSString* key;
@property (nonatomic, retain) NSString* value;
@end

这是一个初始化Restkit的简化代码:

Here's a simplified code to initialize Restkit :

/*
 This part of code must be executed only one time in your application
*/
//To see logs
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);

//Init with good domain
RKObjectManager* manager = [RKObjectManager    objectManagerWithBaseURL:@"http://mydomain.dev/ui/v1"];

//Indicate to use JSON
[RKObjectManager sharedManager].serializationMIMEType = RKMIMETypeJSON;

//Route path when you call a PUT with SOKeyValue class
[manager.router routeClass:[SOKeyValue class] toResourcePath:@"/yourpath" forMethod:RKRequestMethodPUT];

//Serialization for SOKeyValue class
RKObjectMapping* keyvalueSerializationMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class] ];
[authSerializationMapping mapAttributes:@"key", @"value", nil];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:keyvalueSerializationMapping  forClass:[SOKeyValue class] ];

现在我们可以实现使用PUT的服务。在将实现调用的对象中,不要忘记restkit委托 RKObjectLoaderDelegate

Now we can implement a service who use PUT. In the object that will implement the call dont forget the restkit delegate RKObjectLoaderDelegate:

#import <Foundation/Foundation.h>
#import <RestKit/RestKit.h>
#import "SOKeyValue.h"
@interface MyViewOrMyServiceObject: NSObject <RKObjectLoaderDelegate>
- (void)putKeyValue;
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects;
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error;
@end

在你的(.m)中:

- (void)putKeyValue 
{

    SOKeyValue *keyvalue = [[SOKeyValue alloc] init];
    keyvalue.key = @"k";
    keyvalue.value = @"2";
    [[RKObjectManager sharedManager] putObject:keyvalue delegate:self];
    [keyvalue release];
}

您可以在跟踪中看到状态代码,并使用回调函数:

You can see status code in your trace, and use callback functions :

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects;
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error;

所以我家里没有MAC,很难帮你解决代码结构问题。如果您有任何疑问,请不要犹豫。

So i dont have MAC at home, it's diffcult for help you about the code structure. If you have questions do not hesitate.

这篇关于IOS RESTKIT HTTP PUT示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆