NSURLErrorDomain Code = -1012 error

Chandler De Angelis picture Chandler De Angelis · Jun 21, 2013 · Viewed 11.8k times · Source

I am trying to log into a server, but I am receiving an error. I am using SBJSON to convert the request into JSON, and to convert it from JSON back to a string, and I am using one method to make all of my API calls. I've looked around for a solution, but I cannot find any. Here is the method that I use to login:

+ (void)requestFromUrl:(NSString *)urlString withType:(NSString *)type withBody:(NSDictionary *)dict
{
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:type];

    if(dict)
    {
        NSString *jsonString = [dict JSONRepresentation];
        NSData *jsonData = [jsonString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:true];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:jsonData];
    }

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         if (error)
         {
             NSLog(@"Error retrieving data: %@", error.description);
         }
         else
         {
             id result = [data JSONValue];
             NSLog(@"Received responce: %@", result);

             [[NSNotificationCenter defaultCenter] postNotificationName:@"ReceivedResponce" object:self userInfo:result];
         }
     }];

Can someone help me out?

Answer

Stig Brautaset picture Stig Brautaset · Jun 23, 2013

Make this:

    NSData *jsonData = [jsonString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:true];

into:

    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

See if that helps. JSON is required to be UTF8 encoded, not ASCII.