Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always set base URL in WebView #963

Closed
emiloberg opened this issue Oct 20, 2015 · 2 comments · Fixed by #976
Closed

Always set base URL in WebView #963

emiloberg opened this issue Oct 20, 2015 · 2 comments · Fixed by #976
Assignees
Milestone

Comments

@emiloberg
Copy link

Hi,

This issue is highly related to #241 (and fix #625 for that issue). However, as they're closed and delivered I'm opening a new one.

Use case

In my app I'm downloading JSON which is then turned into HTML with a little help from Handlebars - and then served in a WebView. I'm also downloading some images which is shown in that HTML file.

So basically:

wv = new webViewModule.WebView();
wv.src = `
<html>
    <body>
        Hi there, here's a picture
        <img src="some/path/to/my/previously/downloaded/local/image.png">
    </body>
</html>
`

Problem

Setting the src of the WebView to a html string invokes _loadData which invokes

this._android.loadData(src, "text/html; charset=utf-8", "utf-8");

Code here: https://github.com/NativeScript/NativeScript/blob/master/ui/web-view/web-view.android.ts#L97-L103

The problem with WebView's loadData method is that Android doesn't allow retrieving the image in the <img src=.... Console just outputs Not allowed to load local resource.

However, when using loadDataWithBaseURL and setting the baseUrl, Android shows the image just fine.

I monkey-patched web-view.android.ts at line L102 and rewrote that line into:

var baseUrl = "file:///" + require("file-system").knownFolders.documents().path + '/';
this._android.loadDataWithBaseURL(baseUrl, src, "text/html; charset=utf-8", "utf-8", null);

This makes the images show (yay!). But hard coding it to use the documents path as the baseURL root isn't all that nice and I'm not sure how to solve it in a less opinionated way. Otherwise I'd have sent a PR.

Is knownFolders.documents() always a child of knownFolders.currentApp()? If so we could set the baseURL to currentApp() and then let the user prepend the <img src> src with documents() (and possible subdirectories) if the files are stored there, or just prepend it with a fixed path if the files are included in the actual app.

emiloberg pushed a commit to emiloberg/oppna-program-reklistan-app that referenced this issue Oct 20, 2015
@enchev enchev self-assigned this Oct 21, 2015
@enchev enchev added this to the 1.5 milestone Oct 21, 2015
@enchev
Copy link
Contributor

enchev commented Oct 22, 2015

Hey @emiloberg,

In my opinion currentApp is much better since in most cases you will keep your application images inside your app.

Let me know what you think!

Thanks

@enchev enchev added the ready for test TSC needs to test this and confirm against live production apps and automated test suites label Oct 22, 2015
@vchimev vchimev added done and removed ready for test TSC needs to test this and confirm against live production apps and automated test suites os: android labels Oct 30, 2015
@lock
Copy link

lock bot commented Aug 30, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked and limited conversation to collaborators Aug 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants