Search code examples
htmliosobjective-ciframeuiwebview

My iframe does not work with a UIWebView


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>

Solution

  • 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

    enter image description here

    Hope this helps