IOS RESTKIT HTTP PUT example

空扰寡人 提交于 2019-12-06 10:51:10

问题


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.

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


回答1:


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

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

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

In your (.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;

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



来源:https://stackoverflow.com/questions/10078570/ios-restkit-http-put-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!