Youtube API自动完成搜索

时间:2012-11-24 20:11:21

标签: iphone objective-c search autocomplete youtube-api

我正在使用Youtube API,我想拥有一个搜索自动完成功能,就像在网站中输入一样,当您在iPhone App的搜索输入框中输入内容时,它会为您提供术语建议。我已经阅读了文档,但仍然遗漏了,这是否可以使用API​​?

2 个答案:

答案 0 :(得分:4)

嗯,我知道在这里回答已经太晚了,但我会发布这个答案,因为它让我疯狂了几天!并希望它能拯救他人......

所以......我正在使用这个 API http://suggestqueries.google.com/complete/search?client=youtube&ds=yt&alt=json&q=%@ (q是自动完成搜索的查询)。

现在,如果您尝试打开浏览器,请粘贴此API并将q =%@更改为(假设):q = br,您会注意到一些带有后缀.js的文件会下载到您的计算机上。 出于某种原因,我无法像那样解析 JSON ,所以我做了这个伎俩:

    @property(strong, nonatomic) NSMutableArray *ParsingArray // Put that in .h file or after @interface in your .m file

  -(void)autocompleteSegesstions : (NSString *)searchWish{
//searchWish is the text from your search bar (self.searchBar.text)


NSString *jsonString = [NSString stringWithFormat:@"http://suggestqueries.google.com/complete/search?client=youtube&ds=yt&alt=json&q=%@", searchWish];
    NSString *URLString = [jsonString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // Encoding to identify where, for example, there are spaces in your query.


NSLog(@"%@", URLString);

    NSData *allVideosData = [[NSData alloc]initWithContentsOfURL:[[NSURL alloc]initWithString:URLString]];

NSString *str = [[NSString alloc]initWithData:allVideosData encoding:NSUTF8StringEncoding];
NSLog(@"%@", str); //Now you have NSString contain JSON. 
NSString *json = nil;
NSScanner *scanner = [NSScanner scannerWithString:str];
[scanner scanUpToString:@"[[" intoString:NULL]; // Scan to where the JSON begins
[scanner scanUpToString:@"]]" intoString:&json];
//The idea is to identify where the "real" JSON begins and ends.
json = [NSString stringWithFormat:@"%@%@", json, @"]]"];
NSLog(@"json = %@", json);


NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] //Push all the JSON autocomplete detail in to jsonObject array.
                                                                                 options:0 error:NULL];
self.ParsingArray = [[NSMutableArray alloc]init]; //array that contains the objects.
for (int i=0; i != [jsonObject count]; i++) {
    for (int j=0; j != 1; j++) {
        NSLog(@"%@", [[jsonObject objectAtIndex:i] objectAtIndex:j]);
        [self.ParsingArray addObject:[[jsonObject objectAtIndex:i] objectAtIndex:j]];
        //Parse the JSON here...

    }

}}   

那就是它。现在ParsingArray是包含来自youTube的所有自动完成信息的数组!每次用户点击searchBar上的另一个字符时,都可以更改它,使用此功能:

            - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    [self autocompleteSegesstions:self.searchBar.text];}

现在,这是你应该拥有的主要代码。为了使这段代码更快(因为您现在可以看到键盘上有写入延迟),请使用另一个线程下载 ParsingArray 或使用异步块。 (只需将第一个方法的内容插入Async块...)

请记住 - 也许还有另一种方法可以实现自动完成youTube搜索,这比我更好,但我没找到它,我搜索了很多!如果有人知道更好的方式,如果他在这里发布,我会更高兴。

玩得开心!!!

答案 1 :(得分:0)

不是Youtube API - 但您可以使用Google Suggest API。拨打此网址:

http://suggestqueries.google.com/complete/search?client=youtube&ds=yt&q=QUERY

这将返回您的应用可以解析和显示的建议条款的json响应。如果您更喜欢XML到json,请更改

client=youtube

output=toolbar

(保留其余参数相同)。