10

For playing youtube videos, I'm using

-(void)playYoutubeVideo:(NSString *)youtubeUrl {
  NSString *videoId =  [self extractYoutubeIdFromLink:youtubeUrl];
  NSLog(@"video Id %@",videoId);
  [self playVideoWithId:videoId];
}

- (NSString *)extractYoutubeIdFromLink:(NSString *)link {
  NSString *regexString = @"((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)";
  NSRegularExpression *regExp = [NSRegularExpression regularExpressionWithPattern:regexString
                                                                        options:NSRegularExpressionCaseInsensitive
                                                                          error:nil];
  NSArray *array = [regExp matchesInString:link options:0 range:NSMakeRange(0,link.length)];
  if (array.count > 0) {
    NSTextCheckingResult *result = array.firstObject;
    return [link substringWithRange:result.range];
  }
  return nil;
}

- (void)playVideoWithId:(NSString *)videoId {
  NSString *youTubeVideoHTML = @"<html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = 'http://www.youtube.com/player_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'%f', height:'%f', videoId:'%@', events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>";
  NSString *html = [NSString stringWithFormat:youTubeVideoHTML,300,200, videoId];    
  [webView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];

}

I'm getting youtube video ID from the watch url.

Its working fine for the urls like https://www.youtube.com/watch?v=p9mddDM43wE

But, the below youtube video is not playing in UIWebView

https://www.youtube.com/watch?v=CT-fPP1fjDA

Showing the below error,

enter image description here

What might be problem?
Can anyone please clarify?

Nazik
  • 8,696
  • 27
  • 77
  • 123
  • see this once may be it help you http://stackoverflow.com/questions/11509164/youtube-video-id-from-url-objective-c – Anbu.Karthik Aug 04 '16 at 07:17
  • @Anbu.Karthik, tried all regex s nothing is working for an embedded url, I've used the same code that posted in that post, no luck...:( – Nazik Aug 04 '16 at 07:32
  • You would have to ask the URL shortener what the real URL, which contains the Id, actually is. Or you could implement an `NSURLConnectionDelegate` that handles redirects to capture the Id when you get the redirect from the shortener. – Avi Aug 04 '16 at 07:34
  • @Avi, question edited with the actual problem. – Nazik Aug 04 '16 at 08:29
  • @Anbu.Karthik, question edited with the actual problem – Nazik Aug 04 '16 at 08:29
  • The actual problem was understood. – Avi Aug 04 '16 at 08:58
  • is ios9 can not play?use wkwebview – wg_hjl Aug 08 '16 at 10:43
  • if ios9 use WKWebView else use uiwebview ,try it maybe – wg_hjl Aug 08 '16 at 10:45
  • @wg_hjl, tried WKWebview, same issue... :( – Nazik Aug 08 '16 at 13:10
  • 3
    It looks like a YouTube issue rather than an iOS SDK issue as you get same result if you open the video within the native Safari app. Some videos on YouTube aren't made available on mobile by the uploader so it may be that. – George McDonnell Aug 10 '16 at 17:37
  • 2
    yeah @GeorgeMcDonnell is right. Some video doesn't work on mobile. – Minkle Garg Aug 12 '16 at 13:43
  • Since it's getting desperate... **(1)** Test by opening your link as `https://www.youtube.com/embed/CT-fPP1fjDA?vq=small` in mobile browser or webView load (direct URL not as HTML string, since just quick testing)... or try... **(2)** Try using **SFSafariViewController** instead of **webView** to load the video page. – VC.One Aug 14 '16 at 13:32
  • There is restriction in some videos where you're not able to play the video but it was playable to the youtube app. I have also encountered this one. This is also common to Vevo video. – eNeF Nov 18 '16 at 13:05

1 Answers1

2

Recently I faced the same issue in my iOS application where we are loading a YouTube video link inside UIWebView.

After breaking head for long time, it turned out to be the device "date and time" settings problem.

The device date was set to very old. After we updated the settings, the youtube videos started loading inside web view.

Rashmi Ranjan mallick
  • 6,390
  • 8
  • 42
  • 59