We were loading a HTML5 game into an Android webview (Webview renderer in Forms App to be specific – doesn’t matter though) in a Xamarin Forms application and we wanted to save the state of the game in our application.
That is, if the user was at level 5, exited the game or navigated to another page and came back, game shouldn’t start from beginning but from the same level or may be even the same points (as long as backend supports that).
Of course, it was difficult to get a direct sample on Xamarin to fix this issue but 2 sources helped us. We combined both to fix the issue.
- Source 1 : How do we use Local Storage and cookies to save HTML5 games? – Link
- Source 2: Using Android webview and storage – Link
Websites typically uses Local Storage to get this information stored and then share it with server when it loads (and not cookies). Our reference was HTML5 Games Site – http://html5games.com/Game/Zombie-Massacre/3223c9b3-f528-4604-86a1-ed0daca4c285 ). We used Chrome developer tools to see its local storage and information stored for levels while playing to understand the process. Well, we are all newbies in certain areas, of course. We realized that LocalStorage was leveraged and were able to manipulate levels from the developer tools view or SQLite browser (Local Storage file inside windows Locals).
We were able to save HTML5 games in Xamarin Forms Android WebView using local Storage. But, we haven’t implemented it yet on other platforms like iOS. My guess is that the steps should remain pretty much the same regardless of the platform. Given here is an Android sample.
Overall code is actually pretty less to make this working – the minimal code to save the level and points to local storage.
Control in the code below is your Android WebView control.
WebSettings settings = Control.Settings; settings.DomStorageEnabled = true; settings.DatabaseEnabled = true; settings.JavaScriptEnabled = true; string databasePath = this.Context.GetDir("database", FileCreationMode.Private).Path; settings.DatabasePath = databasePath;
I would surely love to hear from you, if you have managed to achieve this in some other way, a better way.
~Trilok R~