objective c - How to setup Basic authentication in RestKit with Core Data? -
i'm using restkit make access rest api. showed in tutorial decided use code data storing results rest api. ok have trouble setup basic authentication restkit. have code:
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];
i can't find place add login , password information. in answers on site found information rkobjectmanager: [[rkobjectmanager sharedmanager].httpclient setauthorizationheaderwithusername:@"1" password:@"1"];
but how use in case?
edit: found solution. instead of code above use this:
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];
i found solution. instead of code above use this:
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];
the main idea use rkobjectmanager requests. can automatically generate request self contained information.
Comments
Post a Comment