2

I am trying to load an HTML file on WebView. The HTML contains image URLs. When I try to run that image URL on Web Browser, it works. The image gets displayed. But the same image does not get loaded in WebView in my app.

I found this question on Apple Developer Forum, it is still unanswered. Any help would be appreciated. Thanks

Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49

2 Answers2

3

I faced a similar issue a few months ago, and I fixed it by replacing UIWebView with WKWebView.

WKWebView is the official replacement of UIWebView, take a look at its documentation:

Starting in iOS 8.0 and OS X 10.10, use WKWebView to add web content to your app. Do not use UIWebView or WebView.

Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
  • Hello, I am facing same issue with WKWebView also. Actually, the HTML I am using is the Page Source of some random webPage. If this can be the issue for images not getting displayed ? – Sushil Sharma Jul 15 '17 at 05:39
  • The images that I stored locally in XCode are displayed in WebView. The issue is with images that are needed to fetch from URL in HTML. – Sushil Sharma Jul 15 '17 at 05:40
  • This is weird. Using `WKWebView` was enough for me. Take a look at the discussion here, it may help you: https://stackoverflow.com/questions/39901982/wkwebview-fails-to-load-images-and-css-using-loadhtmlstring-baseurl – Mo Abdul-Hameed Jul 15 '17 at 07:35
  • In my case i tried first using webview but not loaded images it shows boxes instead of images. then i tried wkwebview it also not shown images. – Protocol May 13 '19 at 06:25
2

Please try to fetch your images using WKWebView.

Step 1: import WebKit in your class

import WebKit

Step 2: Add WebKit delegate to your controller

class ViewController: UIViewController, WKUIDelegate

Step 3: create variable of WKWebView type

var webView: WKWebView!

Step 4: Configure WKWebView. Override loadView() function with following line of code

    let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView

Step 5: In viewDidLoad() pass URLRequest to webView()

let url = "your URL"
let request = URLRequest(url: url!)
webView.load(request)
Vaibhav Parmar
  • 461
  • 4
  • 14