3

我遵循这个旧答案的步骤。主要问题是 RKObjectLoaderDelegate 已被删除,我找不到任何适合我的替代方案。我正在尝试使用邮件/密码连接到服务器,将发布请求发送到 json。另外,在我测试它是否正确后,我将使用 json 从数据库中获取数据。有人可以帮我吗?我正在使用 RESTkit

    //Autentificacion
NSURL* url = [[NSURL alloc]initWithString:@"http://myurl.com/tokens.json"];
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:url];
objectManager = [RKObjectManager managerWithBaseURL:url];

就像我之前说的,我试图做一个 JSON 登录帖子,但不同的例子对我不起作用,我在 restkit 0.20 上寻找一些简单的方法来做。我知道如何做 GET 但不知道 POST。

4

2 回答 2

11

首先,我创建了一个名为 Login Request 的类。

登录请求.h

#import <Foundation/Foundation.h>
#import "RKObjectMapping.h"

@interface LoginRequest : NSObject
@property (nonatomic, strong) NSString* email;
@property (nonatomic, strong) NSString* password;

+(RKObjectMapping*)defineLoginRequestMapping;
@end

登录请求.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;

}

然后,我创建了一个 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

登录管理器.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);

                   }];
}

最后,我在 MasterViewController.m 上调用 viewDidLoad 上的方法

[[LoginManager alloc] LoginWithUserName:@"mymail@test.com" password:@"mypassword"];
于 2013-11-05T10:36:23.397 回答
-1

我猜 Restkit 已经包含了 AFNetworking 2.0 库。所以我认为这会起作用:

RestKit 是一个现代的 Objective-C 框架,用于在 iOS 和 Mac OS X 上实现 RESTful Web 服务客户端。它提供了一个强大的对象映射引擎,与 Core Data 无缝集成,并提供了一组简单的网络原语,用于映射构建在上面的 HTTP 请求和响应AFNetworking _

帖子示例:

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);
}];
于 2013-10-28T10:45:36.317 回答