user2966586
user2966586

Reputation: 165

Load youtube URL in Xcode 5

I have been looking at numerous tutorials to play youtube videos via the app. However they are producing a blank screen. According to comments on the tutorials this is common on xocde 5? Is there another way in which videos can be streamed? I am running it on a iphone and have connected the webview.

I am using :

.h

@interface detailViewController : UIViewController {

    IBOutlet UIWebView *webView;
}

VIEW DID LOAD

      NSURL *url = [NSURL URLWithString:@"<iframe width=\"560\" height=\"315\" src=\"//www.youtube.com/embed/AmMF7hWrYZk\" frameborder=\"0\" allowfullscreen></iframe>"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestURL];

Upvotes: 1

Views: 4537

Answers (6)

Eric
Eric

Reputation: 11

I had the same issue happen when I was using the YouTubeHelper Xcode libraries (https://developers.google.com/youtube/v3/guides/ios_youtube_helper). I found that the first time I added the YT* libraries into my project that I didn't select the "Create Folder References for any added folders" when adding the assets folder into my Xcode project. After removing the existing assets folder and following that step I was able to get the helper libraries to work within my project and play YouTube videos as the link above describes. Hope that helps.

Upvotes: 1

Casper
Casper

Reputation: 3805

I had the same problem. You forgot to put "http://" in your youtube link.

Here's the correct code:

NSURL *url = [NSURL URLWithString:@"<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/AmMF7hWrYZk\" frameborder=\"0\" allowfullscreen></iframe>"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestURL];

Upvotes: 0

Divya Bhaloidiya
Divya Bhaloidiya

Reputation: 5064

Send you NSURLRequest request from

-(void)viewWillAppear:(BOOL)animated {

}

Here is my code, used in my app

NSString *youTubeID = [self extractYoutubeID:url];
 NSString *embedHTML =[NSString stringWithFormat:@"\
                          <html><head>\
                          <style type=\"text/css\">\
                          body {\
                          background-color: #666666;\
                          padding:%f %f %f %f;\
                          color: blue;\
                          }\
                          </style>\
                          </head><body style=\"margin:0\">\
                          <iframe height=\"%f\" width=\"%f\" title=\"YouTube Video\" class=\"youtube-player\" src=\"http://www.youtube.com/embed/%@\" ></iframe>\
                          </body></html>",paddingTop,paddingRight,paddingBottom,paddingLeft,videoHeight,videoWidth,youTubeID];
[self.webView loadHTMLString:embedHTML baseURL:nil];



// extractYoutubeID
+ (NSString *)extractYoutubeID:(NSString *)youtubeURL
{
    //NSLog(@"youtube  %@",youtubeURL);
    NSError *error = NULL;
    NSString *regexString = @"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:NSRegularExpressionCaseInsensitive error:&error];
    NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:youtubeURL options:0 range:NSMakeRange(0, [youtubeURL length])];
    if(!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0)))
    {
        NSString *substringForFirstMatch = [youtubeURL substringWithRange:rangeOfFirstMatch];
        return substringForFirstMatch;
    }
    return nil;
}

Upvotes: 0

BalaChandra
BalaChandra

Reputation: 652

UIWebView *web;
NSString *pageHTML = [[NSBundle mainBundle] pathForResource:@"url" ofType:@"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:pageHTML   encoding:NSUTF8StringEncoding error:nil];
[web loadHTMLString:htmlString baseURL:nil];

the code in url.html is

<html>
   <head></head>
<body>
    <iframe src='http://player.vimeo.com/video/73198189?title=1&byline=0&portrait=0&loop=1' width='690' height='500' frameborder='10' webkitAllowFullScreen mozallowfullscreen allowFullScreen>myframe</iframe>
</body>

i have used this code for playing vimeo videos and it worked.. hope this helps you..

Upvotes: 1

Warren Burton
Warren Burton

Reputation: 17378

Consider letting the web view tell you why its not loading

Implement UIWebViewDelegate methods in your view controller , and attach the delegate to your view controller.

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

may give you a very good hint.

Upvotes: 0

Vinay Jain
Vinay Jain

Reputation: 2634

I had implemented youtube videos using the HCYoutubeParser. Very nice and easy to use.

Upvotes: 0

Related Questions