我已经根据我在这里找到的内容实现了一个非常简单的 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 设备上使用的语言?