0

I would like to to get fetch JSON from http://mycompany.com/page1...http://mycompany.com/page2... On the the webserver side, it requires initial login http://mycompany.com/login, and after that a cookie is maintained for the user. How do I get this behavior with NSURLConnection without having to ask for login every time? Here is the non-working code using NSURLCredential Storage. Do I need to get the cookie from webservice at loging and then send it along with later requests? I struggled with this for some time, So can you please clarify your answer.

- (IBAction)getJSON:(id)sender
{


NSURLCredential *credential = [NSURLCredential credentialWithUser:@"user"
                                                         password:@"pass"
                                                          persistence:NSURLCredentialPersistenceForSession];

NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
                                         initWithHost:@"myCompany.com"
                                         port:0
                                         protocol:@"http"
                                         realm:nil
                                         authenticationMethod:nil];

[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential
                                                    forProtectionSpace:protectionSpace];


//////////GET JSON//////////////

NSError *error;
NSURL *url = [NSURL URLWithString:@"http://mycompany.com.jsonpage1"];
 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}

 //I am NOT getting JSON in this delegate
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSString *responseString = [[NSString alloc] initWithData:responseData    encoding:NSUTF8StringEncoding];

NSLog(@"%@",responseString);

}
ironman2012
  • 19
  • 2
  • 6
  • I would grab the cookie for the user at each app launch by sending their login information (if they wish to save it within the app) automatically. – WhoaItsAFactorial Aug 20 '12 at 11:28
  • @Jeremy, So, it sounds like I need to grab the cookie first, and then send this cookie along with requests. Do you know how can I send the cookie with URLRequests? Thanks – ironman2012 Aug 20 '12 at 11:53

1 Answers1

0

Reading cookies:

refer to Managing HTTP Cookies on iPhone

Setting cookie:

... set dictionary with cookie properties, then:

NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:[NSDictionary dictionaryWithObjects:object forKeys:keys]];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];

but keep in mind that session cookies can expire on your server

Community
  • 1
  • 1
CSmith
  • 13,318
  • 3
  • 39
  • 42
  • Thanks. I think I just need this at app startup to hit may be 2 or 3 links, download JSOn data to my coredata, then at the end of the app, I just need to post data to web server to update data on the server. So, a user can take a day before posting the data to the server. I am sure the cookie will expire by then. How do you think I can handle it? Create another login and cookie when posting the data to server? – ironman2012 Aug 20 '12 at 15:18
  • authentication is a pretty advanced topic ... from logins to digital certificates, there are a number of techniques you could use to accomplish what you want. Either way, you'll need to [re]authenticate when doing your data update. – CSmith Aug 20 '12 at 15:23