如何验证youtube视频以检查视频是否存在URL? [英] How to validate youtube videos to check video exist in URL or not?

查看:65
本文介绍了如何验证youtube视频以检查视频是否存在URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个iPhone开发的新手.目前,我面临验证YouTube视频的问题.我从SOL服务器获取YouTube URL.我只想检查该视频是否存在于YouTube服务器中.我知道可以通过YouTube视频ID进行检查.但是我找不到任何示例代码.有人建议我一种更好的方法还是提供示例代码?

I am new to this iPhone development.Currently I am facing a problem to validate the YouTube videos . I am getting the YouTube URL from SOL server.I just want to check whether that video exists in YouTube server.I came to know that this can be check through YouTube video id .But i could not find any sample code for this.Can any one suggest me a better method or give a sample code?

推荐答案

Youtube具有数据api

Youtube has data api

例如,您可以执行以下操作:

For example you can do something like this:

1)您具有格式为 http://www.youtube.com的视频的完整URL./watch?v = uCYTpCYLqbs 其ID为 uCYTpCYLqbs
2)向 http://gdata.youtube.com/feeds/api/videos/<VIDEO ID>?v = 2& alt = json 发出请求.在我们的例子中是: http://gdata.youtube.com/feeds/api/videos/uCYTpCYLqbs?v=2&alt=json
3)如果视频有效,您将收到一些有用的信息.否则,您会得到一个错误.

1) You have full url to a video in format http://www.youtube.com/watch?v=uCYTpCYLqbs Its id is uCYTpCYLqbs
2) Make request to http://gdata.youtube.com/feeds/api/videos/<VIDEO ID>?v=2&alt=json. In our case it is: http://gdata.youtube.com/feeds/api/videos/uCYTpCYLqbs?v=2&alt=json
3) If the video is valid you'll receive some usefull information about it. Otherwise you'll get an error.

代码可以像这样:

- (BOOL)isValid:(NSURL *)youtubeVideoURL
{
  NSString *urlStr = [youtubeVideoURL absoluteString];
  // retrieve id param from url (ex http://www.youtube.com/watch?v=uCYTpCYLqbs)
  NSString *ID = [urlStr substringFromIndex:[urlStr rangeOfString:@"="].location + 1];


  NSString *apiMethodStr = [NSString stringWithFormat:@"http://gdata.youtube.com/feeds/api/videos/%@?v=2&alt=json", ID];
  NSString *fetchedProperies = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiMethodStr] encoding:NSUTF8StringEncoding error:nil];

  return fetchedProperies != nil;
}

您可以在 stringWithContentsOfURL:encoding:error:中使用 error 参数来更准确地处理错误.或使用NSURLRequest.无论如何,主要思想是相同的.

You can use error param in stringWithContentsOfURL:encoding:error: to handle error more accurate. Or use NSURLRequest. Anyway the main idea would be the same.

这篇关于如何验证youtube视频以检查视频是否存在URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆