我的 iframe 不适用于 UIWebView

My iframe does not work with a UIWebView

我已经在所有地方测试了我的 iframe,它运行良好,但是在 Objective-C 中的 iOS 上,它在 UIWebView 上不起作用,这是我的代码, 有人能帮我吗?谢谢

self.webView.scrollView.scrollEnabled = NO;

NSString *Str = [NSString stringWithFormat:@"<iframe frameborder=\"0\" width=\"359\" height=\"200\" src=\"//www.dailymotion.com/embed/video/%@\" allowfullscreen></iframe>", identifier];

[_webView loadHTMLString:Str baseURL:nil];

我的 iframe :

<iframe frameborder="0" width="359" height="200" src="//www.dailymotion.com/embed/video/x5b4cfz" allowfullscreen></iframe>

您需要将此密钥添加到您的 info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

我发现的主要问题是基数 Url,在您的代码中缺失,因此添加此代码基数 url @"http://www.dailymotion.com",并更改加载方式 html 从 loadHTMLStringloadData 这对我来说总是有更好的结果

已编辑:改进您的代码以处理 WebView 的宽度,因为 @Mozahler 建议是错误的

self.webView.scrollView.scrollEnabled = NO;

NSString *identifierTest = @"x5b4cfz";

NSString *Str = [NSString stringWithFormat:@"<iframe frameborder=\"0\" width=\"%@\" height=\"200\" src=\"//www.dailymotion.com/embed/video/%@\" allowfullscreen></iframe>",[NSString stringWithFormat:@"%f",self.webView.frame.size.width - 10], identifierTest];

NSLog([NSString stringWithFormat:@"%f",self.webView.frame.size.width - 10]);

[_webView loadData:[Str dataUsingEncoding:NSUTF8StringEncoding]
          MIMEType:@"text/html" textEncodingName:@"UTF-8"
           baseURL:[[NSURL alloc] initWithString:@"http://www.dailymotion.com"]];

如您所见,它有效

希望对您有所帮助