My iframe does not work with a UIWebView

我的未来我决定 提交于 2020-01-17 06:49:11

问题


I have tested my iframe everywhere and it works very well, but on iOS in Objective-C, it does not work on UIWebView, here is my code, can someone help me? Thanks

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];

My iframe :

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

回答1:


You need to add this Key to your info.plist

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

The main problem that I found is the base Url, was missing in your code, so add this code with base url @"http://www.dailymotion.com", and changed the way to load html from loadHTMLString to loadData this always have better results for me

Edited: Improved your code to handle width of WebView as @Mozahler suggest was wrong

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"]];

it works, as you can see

Hope this helps



来源:https://stackoverflow.com/questions/44676297/my-iframe-does-not-work-with-a-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!