1

I am getting a string response from the Google Directions API that contains characters that are supposed to represent a different-language character. Similiar to Ƨ³, similiar to these type of characters. I think the characters are portugese, but anyways my string contains like this:

\U00e3o

(I am not sure if those are zeroes or 'O's)

I am not sure what the technical term for these characters are, but how can I fix them in my string so they print properly.

Thank you

UPDATE:

I have updated my question title with the correct term 'unicode'.

I have checked a few questions:

NSString Unicode display

Detect Unicode characters in NSString on iPhone

iOS HTML Unicode to NSString?

And a few others. I have followed the answer, but the unicode characters are not fixed.

UPDATE:

Here is my code to get the response from GDirection.

Forming the request and getting a response:

        AFHTTPClient *_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]];

    [_httpClient registerHTTPOperationClass: [AFJSONRequestOperation class]];

    [_httpClient setDefaultHeader:@"Accept" value:@"application/json"];

    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];

    [parameters setObject:[NSString stringWithFormat:@"%f,%f", location.coordinate.latitude, location.coordinate.longitude] forKey:@"origin"];

    [parameters setObject:[NSString stringWithFormat:@"%f,%f", location2.coordinate.latitude, location2.coordinate.longitude] forKey:@"destination"];

    [parameters setObject:@"false" forKey:@"sensor"];

    [parameters setObject:@"driving" forKey:@"mode"];

    [parameters setObject:@"metric" forKey: @"units"];

    NSMutableURLRequest *request = [_httpClient requestWithMethod:@"GET" path: @"maps/api/directions/json" parameters:parameters];

    request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;

    AFHTTPRequestOperation *operation = [_httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSInteger statusCode = operation.response.statusCode;

        if (statusCode == 200) {

            [self parseResponse:responseObject];

        } else {

        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];

    [_httpClient enqueueHTTPRequestOperation:operation];
}

Retrieving information from response object:

- (void)parseResponse:(NSDictionary *)response {

NSString *status = [response objectForKey: @"status"];

if (![status isEqualToString: @"OK"]) {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat: @"Google Directions Response Status: %@", status] delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];

    [alert show];
}
else {

NSArray *routes = [response objectForKey:@"routes"];

NSDictionary *routePath = [routes lastObject];

if (routePath) {

    NSString *overviewPolyline = [[routePath objectForKey: @"overview_polyline"] objectForKey:@"points"];

    legs = [routePath valueForKey: @"legs"];

    if (legs) {

/* DIRECTION SET ================================================================================================================================ */

        directionOverview = [[NSMutableDictionary alloc] init];

        NSString *legsDis = [NSString stringWithFormat: @"%@", [[legs valueForKey: @"distance"] valueForKey: @"text"]];

        NSString *kmDistance = [self cutStringToPreference: legsDis];

        if (kmDistance) {

            [directionOverview setObject:kmDistance forKey: @"distance"];

            milesLabel.text = kmDistance;

            milesLabel.font = [UIFont fontWithName:@"interstate" size: 20.0];
        }

        NSString *durationText = [NSString stringWithFormat: @"%@", [[legs valueForKey: @"duration"] valueForKey: @"text"]];

        durationText = [self cutStringToPreference: durationText];

        if (durationText) {

            [directionOverview setObject:durationText forKey: @"duration"];
        }                
            NSString *startAddress = [NSString stringWithFormat: @"%@", [legs valueForKey: @"start_address"]];

            startAddress = [self cutStringToPreference: startAddress];

            NSString *endAddress = [NSString stringWithFormat: @"%@", [legs valueForKey: @"end_address"]];

            endAddress = [self cutStringToPreference: endAddress];

            [directionOverview setObject:startAddress forKey: @"origin"];
            [directionOverview setObject:endAddress forKey: @"destination"];

        NSArray *steps = [legs valueForKey: @"steps"];

        if (steps) {

            instructionArray = [[NSMutableArray alloc] init];
            durationArray = [[NSMutableArray alloc] init];
            distanceArray = [[NSMutableArray alloc] init];

            int number = [[[steps lastObject] valueForKey: @"html_instructions"] count];

            for (int i = 1; i <= number; ++i) {

                    NSString *instruction = [[[steps lastObject] valueForKey: @"html_instructions"] objectAtIndex: i-1];

                    instruction = [self cutStringToPreference: instruction];

                    instruction = [self flattenHTML: instruction];

                    instruction = [self stringByDecodingHTMLEntitiesInString: instruction];

                    [instructionArray addObject: instruction];

                    NSString *distance = [[[[steps lastObject] valueForKey: @"distance"] objectAtIndex: i-1] valueForKey: @"text"];

                    [distanceArray addObject: distance];

                    NSString *duration = [[[[steps lastObject] valueForKey: @"duration"] objectAtIndex: i-1] valueForKey: @"text"];

                    [durationArray addObject: duration];

        }
    }
}

    _path = [self decodePolyLine:overviewPolyline];

    NSInteger numberOfSteps = _path.count;

    CLLocationCoordinate2D coordinates[numberOfSteps];
    for (NSInteger index = 0; index < numberOfSteps; index++) {
        CLLocation *location = [_path objectAtIndex:index];
        CLLocationCoordinate2D coordinate = location.coordinate;

        coordinates[index] = coordinate;
    }

    polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
    [self.mapView addOverlay:polyLine];
}

}

}

Displaying the text in a label:

        NSString *overviewAddressText = [NSString stringWithFormat: @"%@ to %@", [directionOverview objectForKey: @"origin"], [directionOverview objectForKey: @"destination"]];

    overviewAddress.text = overviewAddressText;

UPDATE:

Image of label

So as you can see, in the label, the text contains this kind of substring: S/U00e3o, which is a word that has an unsupported character. How can I fix that so that unicode turns into this: São

Community
  • 1
  • 1
MCKapur
  • 9,127
  • 9
  • 58
  • 101
  • The technical term is [unicode](http://en.wikipedia.org/wiki/List_of_Unicode_characters). – Joe Aug 28 '12 at 13:08
  • You need to show us some more code. Please show how do you call the API, what types do you get in a returned message, and how you print the string with special characters. – Sergey Kalinichenko Aug 28 '12 at 13:08
  • I call google directions api using AFNetworking, I ask for a JSON response. The string response is just the object for key "origin" most specifically the origin of the starting location of the direction, which I have made to be in Sao Paulo. I just assign a labels text to this string – MCKapur Aug 28 '12 at 13:14
  • I have updated my question to unicode, I have also looked at fairly many (5) questions on how to fix this, but with no luck – MCKapur Aug 28 '12 at 13:25
  • Unfortunately, you did not provide the information @dasblinkenlight asked for, so we have no idea what code is running, or why you think it is a problem. – Jody Hagins Aug 28 '12 at 13:31
  • ok lemme update the question with some code – MCKapur Aug 28 '12 at 13:32
  • OK - Now, what are you getting, and why is is a problem -- what do you expect. Specifically, what do you mean when you say you want to "fix" it? – Jody Hagins Aug 28 '12 at 13:50
  • let me update the question with a picture – MCKapur Aug 28 '12 at 13:51
  • Are you trying to unescape a string? http://stackoverflow.com/questions/2831935/how-to-convert-unicode-strings-u00e2-etc-into-nsstring-for-display – Simeon Aug 28 '12 at 14:42
  • thank you the link has answers that worked – MCKapur Aug 29 '12 at 08:39

0 Answers0