NSString *username = @"username";
NSString *password = @"password";
NSURL *loginurl = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.reddit.com/api/login/%@",username]];
NSMutableURLRequest *loginrequest = [NSMutableURLRequest requestWithURL:loginurl];
[loginrequest setHTTPMethod:@"POST"];
NSData *loginRequestBody = [[NSString stringWithFormat:@"api_type=json&user=%@&passwd=%@",username,password] dataUsingEncoding:NSUTF8StringEncoding];
[loginrequest setHTTPBody:loginRequestBody];
NSURLResponse *loginResponse = NULL;
NSError *loginRequestError = NULL;
NSData *loginResponseData = [NSURLConnection sendSynchronousRequest:loginrequest returningResponse:&loginResponse error:&loginRequestError];
NSString *loginResponseString = [[NSString alloc]initWithData:loginResponseData encoding:NSUTF8StringEncoding];
NSLog(@"%@",loginResponseString);
the NSLog prints this: (with some letters replaced)
{“json”: {“errors”: [], “data”: {“modhash”:
“j5hq16ukw2f17a9c153xxxxxxxxxa72ad989c96c904d49a97e”, “cookie”:
“13986184,2012-07-14T12:41:05,349f968b3089af75978xxxxxxxxxxxx761397ba0”}}}
How do i access the modhash and the cookie? I tried
[loginResponseData valueForKey:@"json"];
but it says that the class is not key value coding-compliant for the key json
For anyone looking at this after me, you can check out H2CO3’s solution, but I found that the easiest solution was using NSJSONSerialization to do it in a supported fashion.
worked for me.