Nella mia app, ho un pulsante che, se premuto, consente di guardare un video di YouTube (un trailer di un film). All'interno dell'app, senza avviare il safari. Di seguito puoi vedere uno snippet di codice. Questo codice funziona perfettamente bene con iOS5. Tuttavia, in iOS 6, UIButton in findButtonInView è sempre nullo. Qualche idea quale potrebbe essere la ragione?Riproduzione video YouTube interrotta su iOS6 (funziona perfettamente su iOS5)
youtubeWebView.delegate = self;
youtubeWebView.backgroundColor = [UIColor clearColor];
NSString* embedHTML = @" <html><head> <style type=\"text/css\"> body {background-color: transparent; color: white; }</style></head><body style=\"margin:0\"><embed id=\"yt\" src=\"%@?version=3&app=youtube_gdata\" type=\"application/x-shockwave-flash\"width=\"%0.0f\" height=\"%0.0f\"></embed></body></html>";
NSURL *movieTrailer;
if(tmdbMovie) movieTrailer = tmdbMovie.trailer;
else movieTrailer = [NSURL URLWithString:movie.trailerURLString];
NSString *html = [NSString stringWithFormat:embedHTML,
movieTrailer,
youtubeWebView.frame.size.width,
youtubeWebView.frame.size.height];
[youtubeWebView loadHTMLString:html baseURL:nil];
[self addSubview:youtubeWebView];
- (void)webViewDidFinishLoad:(UIWebView *)_webView {
isWatchTrailerBusy = NO;
[manager displayNetworkActivityIndicator:NO];
//stop the activity indicator and enable the button
UIButton *b = [self findButtonInView:_webView];
//TODO this returns null in case of iOS 6, redirect to the youtube app
if(b == nil) {
NSURL *movieTrailer;
if(tmdbMovie) {
movieTrailer = tmdbMovie.trailer;
} else {
movieTrailer = [NSURL URLWithString:movie.trailerURLString];
}
[[UIApplication sharedApplication] openURL:movieTrailer];
} else {
[b sendActionsForControlEvents:UIControlEventTouchUpInside];
}
}
- (UIButton *)findButtonInView:(UIView *)view {
UIButton *button = nil;
if ([view isMemberOfClass:[UIButton class]]) {
return (UIButton *)view;
}
if (view.subviews && [view.subviews count] > 0) {
for (UIView *subview in view.subviews) {
button = [self findButtonInView:subview];
if (button) return button;
}
}
return button;
}
Sto usando nuova API iFrame HTML (che è il modo consigliato da BTW) per incorporare i video di YouTube e questa tecnica di riproduzione automatica findButtonInView non ha mai funzionato per me (restituisce null). – msk
@MSK hai un collegamento a un tutorial o uno snippet di codice sull'uso degli iframe? thx –
Vedere la mia domanda e rispondere [qui] (http://stackoverflow.com/questions/10385791/youtube-video-autoplay-inside-uiwebview) – msk