gree / unity-webview

zlib License
2.3k stars 694 forks source link

Load images from persistentdatapath #224

Open wakatanka opened 7 years ago

wakatanka commented 7 years ago

Hi, i load my html from an sqlite database on iOS, and all the images relative to this HTML in downloaded on the device in the persistentdatapath. Using

string ImageFolderPath = "file:/" + Application.persistentDataPath.Replace("/", "//").Replace(" ", "%20") + "/manualimages/";
WebViewObject.LoadHTML(myHtml, ImageFolderPath);

The html il loaded correctly, but i can't see any images, are a there a workaround for this?

thanks

KojiNakamaru commented 7 years ago

Replace("/", "//") will replace every / in the string so it should be removed. The following modification works for UIWebView (enableWKWebView:false).

diff --git a/sample/Assets/Scripts/SampleWebView.cs b/sample/Assets/Scripts/SampleWebView.cs
index 9ec7973..f8ab5dc 100644
--- a/sample/Assets/Scripts/SampleWebView.cs
+++ b/sample/Assets/Scripts/SampleWebView.cs
@@ -104,7 +104,10 @@ public class SampleWebView : MonoBehaviour
                 }
                 System.IO.File.WriteAllBytes(dst, result);
                 if (ext == ".html") {
-                    webViewObject.LoadURL("file://" + dst.Replace(" ", "%20"));
+                    //webViewObject.LoadURL("file://" + dst.Replace(" ", "%20"));
+                    webViewObject.LoadHTML(
+                        System.Text.Encoding.UTF8.GetString(result),
+                        "file://" + Application.persistentDataPath.Replace(" ", "%20") + "/");
                     break;
                 }
             }

However, WKWebView forbits file:// URLs for loadHtmlString:baseURL: so it won't load your images. For avoiding the issue, please put the main html under Application.persistentDataPath and call LoadURL() as done in the sample app.

derwaldgeist commented 4 years ago

Hi, I just stumbled upon this.

Does it mean that this package won't work with file:// URLs embedded in a web page as well, unless you switch to UIWebView? I am asking because WKWebView is required by Apple nowadays.

There is a flag to tell WKWebView that it may allow this: https://bugs.webkit.org/show_bug.cgi?id=154916#c7

Is this implemented in this package?

Also wondering how the situation is for Android 10.

(We are currently using UniWebView and have problems with exact this. We need access to a local file that is actually a screen shot of the native Augmented Reality camera. It seems as if this worked in UniWebView before, but now is broken. I was wondering if we could use your package instead.)

KojiNakamaru commented 4 years ago

This plugin just utilizes native Android WebView and/or iOS WKWebView/UIWebView components. The allowFileAccessFromFileURLs seems to work but is not officially supported.

https://stackoverflow.com/questions/36013645/setting-disable-web-security-and-allow-file-access-from-files-in-ios-wkwebvi/41266699#41266699

However, as described above, WKWebView (since iOS9) allows to load local files if you use LoadURL() instead of LoadHTML(). The sample app in this repository actually loads a local sample.html by LoadURL(), and sample.js and sample.jpg, both are referred in sample.html, are correctly loaded.

https://github.com/gree/unity-webview/tree/ac10622fc470d1886444b9462eecd131f3469488/sample/Assets/StreamingAssets

Please note the sample app first writes these files into Applicaiton.persistentDataPath and then load sample.html. You can therefore dynamically generate any webview content locally under Applicaiton.persistentDataPath for your webview.

https://github.com/gree/unity-webview/blob/ac10622fc470d1886444b9462eecd131f3469488/sample/Assets/Scripts/SampleWebView.cs#L119-L148

Android 10 seems to have many troubles about WebView, but these should be reduced by updating Android System WebView from Google Play.