我已根据我在此处找到的内容实施了一项非常简单的Google建议功能:http://shreyaschand.com/blog/2013/01/03/google-autocomplete-api/
您甚至不必使用XML解析器:
+ (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;
}
只有你已经知道两个字母的语言代码才有效。
根据用户搜索的位置,您知道我能猜出两个字母的语言代码吗?
如果它严格基于地理位置,如果让我们说用户已经在法国度假并且突然建议改为谷歌法国,我将不会喜欢它。
它应该基于用户在iOS设备上使用的语言吗?
答案 0 :(得分:0)
此更正实际上将解决问题:
NSString *URLString = [NSString stringWithFormat:@"http://suggestqueries.google.com/complete/search?q=%@&client=toolbar&hl=%@", query, [[NSLocale currentLocale] languageCode]];
尽管如此,居住在印度并将其语言设为法语的人将获得基于Google France的Google建议。
这更像是减轻问题的重量而不是解决问题的重量。