0

I'm using the below code to try and have a youtube video play inside my app (in a UIWebView). For some reason, my WebView returns as just a black box, and no video plays.

See below:

DashboardViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *videoURL = @"https://youtu.be/8QrCPihtBSc";


    UIWebView *videoView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.howtoView.frame.size.width, self.howtoView.frame.size.height)];
    self.howtoView.backgroundColor = [UIColor whiteColor];
    self.howtoView.opaque = NO;
    self.howtoView.delegate = self;
    [self.howtoView addSubview:videoView];


    NSString *videoHTML = [NSString stringWithFormat:@"\
                 <html>\
                 <head>\
                 <style type=\"text/css\">\
                 iframe {position:absolute; top:50%%; margin-top:-130px;}\
                 body {background-color:#000; margin:0;}\
                 </style>\
                 </head>\
                 <body>\
                 <iframe width=\"100%%\" height=\"240px\" src=\"%@\" frameborder=\"0\" allowfullscreen></iframe>\
                 </body>\
                 </html>", videoURL];

    [videoView loadHTMLString:videoHTML baseURL:nil];

1 Answer 1

0
  1. UIWebView is already deprecated, please us WKWebView
  2. There is embed button on youtube web page, that gives you required code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.