【问题标题】:My iframe does not work with a UIWebView我的 iframe 不适用于 UIWebView
【发布时间】:2017-11-24 08:52:03
【问题描述】:

我已经在任何地方测试了我的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>

【问题讨论】:

  • 在某些型号上,您的宽度比 iPhone 的宽度宽,(我不知道这是否是一个真正的问题),我建议您使用 wkwebview 而不是 uiwebview。

标签: html ios objective-c iframe uiwebview


【解决方案1】:

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

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

我发现的主要问题是您的代码中缺少基本 URL,因此使用基本 URL @"http://www.dailymotion.com" 添加此代码,并将加载 html 的方式从 loadHTMLString 更改为 loadData 这总是更好我的结果

已编辑:改进了您的代码以处理 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"]];

如您所见,它有效

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2013-04-17
    • 1970-01-01
    相关资源
    最近更新 更多