WKWebView fails to load images and CSS using loadHTMLString(_, baseURL:)

前端 未结 10 852
抹茶落季
抹茶落季 2020-12-14 14:05

Apple\'s recommendation:

In apps that run in iOS 8 and later, use the WKWebView class instead of using UIWebView.

Thus, I have r

10条回答
  •  北荒
    北荒 (楼主)
    2020-12-14 14:54

    I was able to reproduce a similar issue. WKWebView loads my images specially if they are located remotely, apart from my app server.

    For servers that are not SSL-secured (http instead of https), you can set your info.plist as per below:

    App Transport Security Settings
    
     - Allow Arbitrary Loads in Web Content (Set to YES)
     - Allow Arbitrary Loads (Set to YES)
    

    The problem was actually in the server. The server application was either:

    • Changing the image src from "http://IP-or-domain/uploads/file.jpg" to "../../uploads/file.jpg"

    - OR -

    • The image src was "http://localhost/uploads/file.jpg" or "http://127.0.0.1/uploads/file.jpg" instead of "http://YOUR-SERVER-IP-ADDRESS/uploads/file.jpg"

    In these cases, the actual device wont be able to locate the image. This only works with iOS Simulator because the virtual device is the same as the server and development machine. It can read LOCALHOST and 127.0.0.1.

    In my server, I was using a Rich Text Editor (TinyMCE) and it automatically removes the IP address after it detects that it's the same source.

提交回复
热议问题