WebView and text / raw data
Hi,
Trying to use WebView to view a remote normal text file (content-type: plain/text) but it doesn't seem to work. Renaming the file to .html helps a bit - but then it doesn't display correctly unless I put <html> </html> inside.
What filetypes are supported for the WebKit within Titanium?
3 Answers
-
Turns out downloading anything seems to be fine (txt, html, js, etc… files). The issue is loaded them into a webview, which expects HTML (either via url: or html: attribute).
Solution for text files:
Download the file first, and then load the file from the filesystem, put basic HTML wrapper around it, and then feed it to the WebView as the html attribute.
Code:
var contentfile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'textfile.txt'); if (contentfile.exists()) { var content = contentfile.read(); } var text_in_html = "<html><body><pre>" + content.text + "</pre></body></html>"; // Create our Webview var myWV = Ti.UI.createWebView({ html:text_in_html, title:'Title goes here', left:0, right:0, top:0, bottom:0, loading: true });
-
The short answer is we support anything that the underlying Webview engine supports - which is a little different on each implementation of the OS. Android and iPhone are pretty close but it looks like - for example - Android doesn't support a few mimetypes like SVG and PDF (in the webview) as a mimetype.
I'm surprised that plain text isn't working though. Can you post a sample of what you're trying to do and we can (a) look at it to see if it's a bug - either in our stuff or yours and (b) potentially offer an alternative.
Thanks.
-
Heres the code - 95% cut/paste from Kitchen Sink for iPad + iPhone:
Download = {}; Download.view = Ti.UI.createView(); Download.init = function() { var ind=Titanium.UI.createProgressBar({ width:200, height:50, min:0, max:1, value:0, style:Titanium.UI.iPhone.ProgressBarStyle.PLAIN, top:10, message:'Downloading Technical Data', font:{fontSize:12, fontWeight:'bold'}, color:'#888' }); Download.view.add(ind); ind.show(); var c = Titanium.Network.createHTTPClient(); var b1 = Titanium.UI.createButton({ title:'Download Tech Data', height:40, width:200, top:70 }); Download.view.add(b1); b1.addEventListener('click', function() { var sturl = 'http://ken.bantoft.org/app/works.html'; c.onload = function() { var filename = 'st.html'; var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename); f.write(this.responseData); var wv = Ti.UI.createWebView({ url:f.nativePath, height:850, left:0, right:0, top:150 }); Download.view.add(wv); }; c.ondatastream = function(e) { ind.value = e.progress ; Ti.API.info('ONDATASTREAM1 - PROGRESS: ' + e.progress); }; c.open('GET',sturl); c.send(); }); };
You can change the URL from works.html -> fails.html and see the difference. The only change between the 2 files is I added HTML tags at the top/bottom of the works.html. Now in real life, I can't do that, since I'm pulling data from a remote box I don't control.