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.