【发布时间】:2012-09-09 20:01:13
【问题描述】:
在我的应用程序中,我有一个按钮,按下该按钮后,您可以观看 youtube 视频(电影预告片)。在应用程序内,无需启动 safari。下面你可以看到一个代码sn-p。此代码在 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;
}
【问题讨论】:
-
我正在使用新的 HTML iFrame API(这是 BTW 推荐的方式)来嵌入 youtube 视频,而这种自动播放技术 findButtonInView 从来没有为我工作过(返回 null)。
-
@MSK 您是否有一个教程链接或使用 iframe 的代码 sn-p?谢谢
-
看我的问答here
-
谢谢。所以这意味着不可能在应用内按下按钮来观看 youtube 视频?
-
它从来没有为我工作过,而且它是一种黑客,不能保证它会在下一次操作系统更新时工作。