Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Youtube embedded in UIWebView causes crash on iPad when entering full screen

I'm trying to embed a Youtube video using a mixture of this technique, and this Youtube SDK blog post in a universal app. The iPhone version, using the same code, works fine. On the iPad the video does embed, and it plays fine in it's embedded form, but as soon as you tap the full screen button the app crashes (buttons do not respond, the device does not rotate). The music from the Youtube video keeps playing.

There is no error message logged but the app does register as 'Paused' or hung in xCode. Every time it crashes com.apple.libdispatch-manager is on thread 2. Ask me questions and I'll give you more information about the error, but I'm not sure where to start.

I have tried:

  • changing the size of the UIWebView frame
  • the UIWebView is in a UIScrollView, but if I take it out of the scrollview and add it to the view the problem is identical.
  • changing the video
  • changing the html that I use in the UIWebView from this to this, with no result
  • changing the format of the youtube link from ?v=uniqueID to /v/uniqueID
  • checking the presenting view is the rootviewcontroller (it is, but the video is embedded in a modal, which is not the rootviewcontroller).

I am building for iOS 5.1, this doesn't happen if running on iOS6.

The View that the video is embedded in is modal, both on the phone and the iPad. There's no hackery or unusual things happening in the app.

There seems to be talk of Evernote's app having a similar problem, but I don't know if it is related or not.

For your reference, here is the YouTubeView subclass (which subclasses UIWebView):

- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame;
{
    if (self = [super init]) 
    {
        // Create webview with requested frame size
        self = [[YouTubeView alloc] initWithFrame:frame];

// HTML to embed YouTube video
//      NSString *youTubeVideoHTML = @"<html><head>
//                                       <body style=\"margin:0\">
//                                         <embed id=\"yt\" src=\"%@\"
//                                                type=\"application/x-shockwave-flash\"
//                                                width=\"%0.0f\" height=\"%0.0f\">
//                                         </embed>
//                                       </body>
//                                     </html>";

        NSString *youTubeVideoHTML = @"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = %0.0f\"/></head><body style=\"background:#FFF;margin-top:0px;margin-left:0px\"><div><object width=\"%0.0f\" height=\"%0.0f\"><param name=\"movie\" value=\"%@\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%@\"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"%0.0f\" height=\"%0.0f\"></embed></object></div></body></html>";

        // Populate HTML with the URL and requested frame size
//      NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString, frame.size.width, frame.size.height];

        NSLog(@"html:\n %@", youTubeVideoHTML);

        NSString *html = [NSString stringWithFormat:youTubeVideoHTML, frame.size.width, frame.size.width, frame.size.height, urlString, urlString, frame.size.width, frame.size.height];

        NSLog(@"html:\n %@", html);

        // Load the html into the webview
        [self loadHTMLString:html baseURL:nil];
    }

    return self;
}
like image 794
glenstorey Avatar asked Nov 04 '22 19:11

glenstorey


1 Answers

Modal view on iOS 5.0 and iOS 5.1 is the problem that causes crash on full screen video, AFAIK. They just changed hierarchy of views in that version of iOS (parentViewController and presentingViewController) and that is the aftermath. I asked about it long time ago here and one more same question is here and still no one knows what to do.

First of all, they fixed it in 6.0, I guess, that's good.

For 5.1 we changed design a little and avoided modal view. Do it, if it is possible in your situation.

like image 108
Pavel Oganesyan Avatar answered Nov 08 '22 07:11

Pavel Oganesyan