0

I've implemented a very simple Google suggestions feature based on what I've found here: http://shreyaschand.com/blog/2013/01/03/google-autocomplete-api/

You don't even have to use an XML parser:

+ (NSArray *)suggestionsForQuery:(NSString *)query language:(NSString *)language
{
    NSMutableArray *suggestions;

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

    NSError *error = nil;

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

    if (error) {

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

        XMLString = [XMLString stringByReplacingOccurrencesOfString:@"<?xml version=\"1.0\"?>" withString:@""];

        XMLString = [XMLString stringByReplacingOccurrencesOfString:@"<toplevel>" withString:@""];

        XMLString = [XMLString stringByReplacingOccurrencesOfString:@"</toplevel>" withString:@""];

        XMLString = [XMLString stringByReplacingOccurrencesOfString:@"<CompleteSuggestion>" withString:@""];

        XMLString = [XMLString stringByReplacingOccurrencesOfString:@"</CompleteSuggestion>" withString:@""];

        XMLString = [XMLString stringByReplacingOccurrencesOfString:@"<suggestion data=\"" withString:@""];

        suggestions = [NSMutableArray arrayWithArray:[XMLString componentsSeparatedByString:@"\"/>"]];

        [suggestions removeLastObject];
    }

    return suggestions;
}

It works but only if you already know the two letter language code.

Do you know of any way in which I can guess the two letter language code based on the location the user is searching from?

If it's strictly based on location, I wouldn't like it if let's say a user has gone on vacation in France and all of a sudden suggestions change to Google France.

Should it be based on what language the user is already using on his iOS device?

Vulkan
  • 1,004
  • 16
  • 44

1 Answers1

0

This correction will actually solve the problem:

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

Still, people living in India and having their language set to French will have Google suggestions based on Google France.

This is more like lessening the weight of the problem than solving it.

Vulkan
  • 1,004
  • 16
  • 44