Thursday, 14 July 2011

Getting An Android WebView Set Up

Creating a little WebView inside an android app to run my web app locally on a phone has been a bit of a pain. By default webview is fairly locked down and for a good reason, security. That means to be useful we have to turn lots of things on in order to do anything useful (assuming you want to do something other than display simple html). There the fun begins. Turn on JavaScript, Dom Storage and a host of other things.

Then local storage is not persistent so goes away when when the app closes, not really what you would expect from localStoarage. To get persistent persistent storage you have to google a little more and eventually arrive at this little bit of code like this

String databasePath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();  
webview.getSettings().setDatabasePath(databasePath); 
A little cheer as everything seems to be working. 

Perhaps I would have preferred a function called setUpForLocalWebApp() that turns on all the stuff you need for a local web app but then I guess that would be too open to coding abuse with programmers turning it on when they should not. Still this is code I only have to figure out once and use again and again. 

1 comment: