How to validate youtube videos to check video exist in URL or not?

偶尔善良 提交于 2019-12-13 04:39:38

问题


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?


回答1:


Youtube has data api

For example you can do something like this:

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.

Code can be like this:

- (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;
}

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.



来源:https://stackoverflow.com/questions/10493393/how-to-validate-youtube-videos-to-check-video-exist-in-url-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!