Encoding problems when requesting Google suggestions

489 views Asked by At

I'm trying to get Google Suggestions to work for Greek but I get an error if the suggestions contain characters other than english.

If the query is "magni" for example the response will contain a greek word "μαγνήσιο" as a suggestion. Try it

The error is:

Error Domain=NSCocoaErrorDomain Code=261 "The file “search” couldn’t be opened using text encoding Unicode (UTF-8)." UserInfo={NSURL=http://suggestqueries.google.com/complete/search?q=magni&client=toolbar&hl=el, NSStringEncoding=4}

The method is:

+ (NSArray *)suggestionsForQuery:(NSString *)query
{
    query = [query stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

    NSString *URLString = [NSString stringWithFormat:@"http://suggestqueries.google.com/complete/search?q=%@&client=toolbar&hl=%@", query, @"el"]; // el is for greek

    NSError *error = nil;

    NSString *XMLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:URLString] encoding:NSUTF8StringEncoding error:&error];

    if (error) {

        NSLog(@"Error: %@", error.description);
    }
    else {

        // ...
    }
}

I tried:

  1. Setting the encoding to NSUTF16StringEncoding but I got some Chinese characters.

  2. NSJSONSerialization by setting "toolbar=firefox" to get a JSON response but I got the same error.

Error Domain=NSCocoaErrorDomain Code=3840 "Unable to convert data to string around character 40." UserInfo={NSDebugDescription=Unable to convert data to string around character 40.

Any ideas on how to fix it?

0

There are 0 answers