我实现了如下所示的 Youtube 视图。重要的是,这只能与 youtube 的embed-url的 url 字符串一起使用,它应该如下所示:
<iframe width="420" height="315" src="http://www.youtube.com/embed/ntQDyLpoLWM" frameborder="0" allowfullscreen></iframe>
(上面的所有代码都有一个问题type="application/x-shockwave-flash"
吗?)
这是我的代码:
#import "YoutubeView.h"
@implementation YoutubeView
- (id)initWithFrame:(CGRect)frame andURL:(NSString *)url
{
self = [super initWithFrame:frame];
if (self)
{
urlString = [url retain];
webView = [[UIWebView alloc] initWithFrame:self.bounds];
[webView.scrollView setBounces:NO];
[self addSubview:webView];
}
return self;
}
-(void)layoutSubviews
{
webView.frame = self.bounds;
NSMutableString *html = [[NSMutableString alloc] init];
[html appendString:@"<html>"];
[html appendString:@"<head>"];
[html appendString:@"<meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\">"];
[html appendString:@"</head>"];
[html appendString:@"<body style=\"border:0; margin:0; padding:0;\">"];
[html appendFormat:@"<iframe width=\"%d\" height=\"%d\" src=\"%@\" frameborder=\"0\" allowfullscreen>", (int) roundf(self.bounds.size.width), (int) roundf(self.bounds.size.height), urlString];
[html appendString:@"</iframe>"];
[html appendString:@"</body>"];
[html appendString:@"</html>"];
[webView loadHTMLString:html baseURL:nil];
[html release];
}
-(void)dealloc
{
[webView release];
[urlString release];
[super dealloc];
}
@end