UIWebview未在iFrame中加载YouTube视频

时间:2013-12-04 09:48:33

标签: ios objective-c iframe uiwebview

我正在尝试将HTML字符串加载到UIWebView中。 HTML包含iframe,但不会在网页视图中加载。

这是我的代码,

webDescription = [[UIWebView alloc]initWithFrame:CGRectMake(0, 255,self.view.bounds.size.width, 20)];
[[webDescription scrollView] setBounces: NO];
webDescription.delegate = self;
NSString *myDescriptionHTML = [NSString stringWithFormat:@"<html> \n"
                                   "<head> \n"
                                   "<style type=\"text/css\"> \n"
                                   "body {font-family: \"%@\"; font-size: %@; COLOR:#111111; background-color:transparent; padding:10px 5px 15px 20px;}\n"
                                   "</style> \n"
                                   "</head> \n"
                                   "<body>%@</body> \n"
                                   "</html>", @"MyriadPro-Regular", [NSNumber numberWithInt:12], _group.gro_content];
[webDescription setBackgroundColor:[UIColor clearColor]];
[webDescription setOpaque:NO];
[webDescription loadHTMLString:myDescriptionHTML baseURL:nil];
[scrollView addSubview:webDescription];

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

试试这段代码,

- (void)embedYouTube:(NSString *)youtubeUrl frame:(CGRect)frame {
    NSString *embedHTML = @"\
                                <html><head>\
                            <style type=\"text/css\">\
                            body {\
                                background-color: transparent;\
                                color: white;\
                            }\
                            </style>\
                            </head><body style=\"margin:0\">\
                            <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
                            width=\"%0.0f\" height=\"%0.0f\"></embed>\
                            </body></html>";
    NSString *html = [NSString stringWithFormat:embedHTML, youtubeUrl, frame.size.width, frame.size.height];
    UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
    [videoView loadHTMLString:html baseURL:nil];
    [playerview addSubview:videoView];
}