7

RestKitを使用して、基本認証を必要とするエンドポイントを呼び出そうとしています。

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[JSNCategory class]];
[mapping addAttributeMappingsFromDictionary:@{
    @"id": @"catId",
    @"name": @"name"
}];

NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor
 = [RKResponseDescriptor responseDescriptorWithMapping:mapping
                                           pathPattern:@"/api/v1/categories"
                                               keyPath:nil
                                           statusCodes:statusCodes];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
                         URLWithString:@"https://rest.example.com"]];

RKObjectRequestOperation *operation
  = [[RKObjectRequestOperation alloc] initWithRequest:request
                                  responseDescriptors:@[responseDescriptor]];

[operation setCompletionBlockWithSuccess:
^(RKObjectRequestOperation *operation, RKMappingResult *result) {
    JSNCategory *cat = [result firstObject];
    NSLog(@"Mapped the category: %@", cat);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Failed with error: %@", [error localizedDescription]);
}];
4

1 に答える 1

26

objectmanagerを使用すると、これは次のようになります。

NSURL* url = [[NSURL alloc]initWithString:@"http://rest.url.com"];
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:url];

[objectManager.HTTPClient setAuthorizationHeaderWithUsername:@"username" password:@"password"];

次に、正しい要求/応答を設定した後、objectmanagerを使用してget / post/etcを実行できます。

[objectManager getObjectsAtPath:endpoint parameters:parameters success:
     ^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
         // do something
     }  
     failure:^(RKObjectRequestOperation *operation, NSError *error) {
         // do something
     }
];
于 2012-12-13T12:02:19.213 に答える