1
votes

I am getting an issue while playing a youtube video using webView. I am giving one html string as an input in which youtube video url is provided. My webView is loading successfully. Also in delegate of webViewDidFinishLoad, I can get the proper url using - webView.request.URL.absoluteString

But strange fact is, I cant see any interface except webview's background color. This is happening on iOS 9.0.2. On simulator of iOS 9.0. its working fine.

Also, on the device with iOS 8.3 it's working fine. Only on the device of iOS 9.0.2 it's not working.

Below is the code I wrote so far -

NSString *strLink = [NSString stringWithFormat:@"https://www.youtube.com/embed/%@/clean",strVidID];

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

            [self.webVwVideoPlayer loadHTMLString:youTubeVideoHTML baseURL:baseURL];

where, self.webVwVideoPlayer is outlet to UIWebView.

Also, as per new app transport security, I have modified my plist file as -

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    <key>NSExceptionDomains</key>
    <dict>
    <key>youtube.com</key>
    <dict>
    <!--Include to allow subdomains-->
    <key>NSIncludesSubdomains</key>
    <true/>


    <!--Include to allow insecure HTTP requests-->
    <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
    <true/>


    <!--Include to specify minimum TLS version-->
    <key>NSTemporaryExceptionMinimumTLSVersion</key>
    <string>TLSv1.1</string>
    </dict>
    </dict>
    </dict>

Please let me know if anyone know about this.

2

2 Answers

2
votes

You can use XCDYouTubeVideoPlayerViewController. It seamlessly adds youtube video to your app without the need of UIWebView.

It uses progressive download + MoviePlayer and AVFoundation frameworks. Just supply it with a youtube video ID and you are good to go.

It supports both full-screen & non full-screen and WORKS ON iOS SIMULATOR!!!

Example:

XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"9bZkp7q19f0"];
[self presentMoviePlayerViewControllerAnimated:videoPlayerViewController];
0
votes
 UIWebView *video=[[UIWebView alloc]initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH-20, 180)];
            video.scrollView.scrollEnabled = NO;
            NSString *strHtml = [NSString stringWithFormat:@"<iframe src=\"https://www.youtube.com/embed/%@\" width=\"280\" height=\"185\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>",@"8syP39rL2kY"];
            NSString *htmlStringToLoad = [NSString stringWithFormat:@"https://www.youtube.com/%@",@"8syP39rL2kY"];
            [video loadHTMLString:strHtml baseURL:[NSURL URLWithString:htmlStringToLoad]];
            [self.view addSubview:video];