1

我正在使用 RestKit 来访问我的 REST API。如他们的教程所示,我决定使用代码数据来存储来自我的 REST API 的结果。一切正常,但我无法使用 RestKit 设置基本身份验证。所以我有这个代码:

RKManagedObjectStore *managedObjectStore = [RKManagedObjectStore defaultStore];

RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Product" inManagedObjectStore:managedObjectStore];
    [entityMapping addAttributeMappingsFromDictionary:@{
     @"Id":       @"id",
     @"Category": @"category",
     @"Name":     @"name",
     @"Price":    @"price"}];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping method:RKRequestMethodGET pathPattern:@"/api/products/" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://win8virtual:49876/api/products/"]];    


RKManagedObjectRequestOperation *managedObjectRequestOperation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];

managedObjectRequestOperation.managedObjectContext = self.managedObjectContext;

[[NSOperationQueue currentQueue] addOperation:managedObjectRequestOperation];

我找不到添加登录名和密码信息的地方。在本网站的一些答案中,我找到了有关 RKObjectManager 的信息:[[RKObjectManager sharedManager].HTTPClient setAuthorizationHeaderWithUsername:@"1" password:@"1"];

但是在我的情况下如何使用它?

编辑:我找到了解决方案。而不是上面的代码使用这个:

RKManagedObjectStore *managedObjectStore = [RKManagedObjectStore defaultStore];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://win8virtual:49876"]];
    objectManager.managedObjectStore = managedObjectStore;
 [objectManager.HTTPClient setAuthorizationHeaderWithUsername:@"1" password:@"1"];
[RKObjectManager setSharedManager:objectManager];
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Product" inManagedObjectStore:managedObjectStore];
    [entityMapping addAttributeMappingsFromDictionary:@{
     @"Id":       @"id",
     @"Category": @"category",
     @"Name":     @"name",
     @"Price":    @"price"}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping method:RKRequestMethodGET pathPattern:@"/api/products/" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
// Добавляем описание ответа в менеджер объектов, чтобы он автоматом обрабатывал запросы по url
    [objectManager addResponseDescriptor:responseDescriptor];
// Request!
[[RKObjectManager sharedManager] getObjectsAtPath:@"/api/products/" parameters:nil success:nil failure:nil];
4

1 回答 1

1

我找到了解决方案。而不是上面的代码使用这个:

RKManagedObjectStore *managedObjectStore = [RKManagedObjectStore defaultStore];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://win8virtual:49876"]];
    objectManager.managedObjectStore = managedObjectStore;
 [objectManager.HTTPClient setAuthorizationHeaderWithUsername:@"1" password:@"1"];
[RKObjectManager setSharedManager:objectManager];
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Product" inManagedObjectStore:managedObjectStore];
    [entityMapping addAttributeMappingsFromDictionary:@{
     @"Id":       @"id",
     @"Category": @"category",
     @"Name":     @"name",
     @"Price":    @"price"}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping method:RKRequestMethodGET pathPattern:@"/api/products/" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
// Добавляем описание ответа в менеджер объектов, чтобы он автоматом обрабатывал запросы по url
    [objectManager addResponseDescriptor:responseDescriptor];
// Request!
[[RKObjectManager sharedManager] getObjectsAtPath:@"/api/products/" parameters:nil success:nil failure:nil];

主要思想是使用 RKObjectManager 处理任何请求。它可以通过自包含的信息自动生成请求。

于 2013-09-04T20:17:41.037 回答