如何使用Core Data在RestKit中设置基本身份验证?

时间:2013-09-04 13:24:34

标签: objective-c restkit-0.20

我正在使用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”密码:@“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];

1 个答案:

答案 0 :(得分: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。它可以通过自包含信息自动生成请求。