在我的应用程序中,我有一个按钮,按下该按钮后,您可以观看 youtube 视频(电影预告片)。在应用程序内,无需启动 safari。您可以在下面看到一个代码片段。此代码在 iOS5 下运行良好。但是,在 iOS 6 中,findButtonInView 中的 UIButton 始终为 nil。任何想法可能是什么原因?
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;
}