Saturday, March 24, 2012

Phonegap-SQLitePlugin: reliable offline storage for iOS

UPDATE: Please see my new posting: Cordova/PhoneGap sqlite plugins offer large db size, excellent reliability

There has been a bit of discussion on the Cordova/PhoneGap forum that "LocalStorage or SQLite data will no longer be backup and can be deleted" for HTML5 as of iOS 5.1. This news has given me a very bad feeling while I am developing my first iPhone app and I was immediately searching for a more reliable solution. I tried using the Application Preferences plugin but it did not work very well either on the iPhone simulator or on my iPhone device. I discovered the Phonegap-SQLitePlugin which does seem to provide a much more reliable solution.

There has been quite a bit of effort to solve this problem in the Corvdova/PhoneGap project. Kerri Shotts had been kind enough to contribute a workaround that does the backup and restore tasks during startup, "resign active", and termination phases and I did consider using this workaround very seriously. I chose not to use this workaround because first I would have to do separate testing to verify that this is working both pre-iOS 5.1 and iOS 5.1+ and second I have some doubts about its reliability. I am very concerned that if an app or even an iOS device crashes then some user data could be lost. Apache issue CB-330 had been logged and some work had been done in the incubator-cordova-ios project which still uses the backup-and-restore mechanism.

In general, I like the Phonegap-SQLitePlugin much better due to its reliability. Currently this SQLite Plugin works on PhoneGap 1.3 and earlier but not on Cordova 1.5 due to the changes in the iOS plugin API. The SQLite Plugin exposes an API that is similar but not the same as the HTML5 Web SQL API. In general I have been able to find plenty of answers on the Internet about how to format SQLite queries and the PhoneGap SQLite Plugin API should follow the SQLite C API very closely.

In general I am very happy to use the Phonegap-SQLitePlugin. There are a few issues that I would like to see solved in the near future. Cordova support is currently missing and I have had no time to try to make this work. In addition, it would be nice to see an adaptation or even a wrapper to emulate the HTML5 Web SQL API.

Introduction

Hi my name is Chris Brody and I currently busy getting my first iPhone app ready for a client. We are in a fast-growing business and we see so many people trying to get started building mobile apps and mobile web apps. I have started following the Cordova/PhoneGap forum and I see so many people looking for some really basic information so I was inspired to create this blog to post some important pointers.

As I write my first blog post I will give some simple pointers for a "newbie or n00b" considering how to build a mobile app or a mobile web app.

An increasing number of developers are using HTML5 web programming technology which is actually quite advanced. Javascript is actually quite advanced since it provides support for both object-oriented programming and functional programming. I recommend considering CoffeeScript which can make web programming much more easy and fun; Smooth CoffeeScript is an excellent learning resource.

There are a number of wonderful HTML5 programming libraries; my favorites include JQTouchJQuery Mobile, and JQMobi. For mobile web apps you can use the HTML5 app cache manifest to keep the app running offline and the HTML5 local storage or Web SQL API to store user data.

For a mobile app, the first step is to consider what kind of SDK or set of libraries to use. Some more advanced developers may choose to use a native API; however an increasing number of developers are choosing to use portable libraries due to factors such as ease of use, time to market, etc. For HTML5 technology the most commonly used library is Apache Cordova, also known as PhoneGap. I recommend the PhoneGap/Cordova Crib Sheet for anyone looking for resources to help with programming either for an app store or for mobile web apps.

I had been programming mobile messaging systems for +/- 13 years in C and C++ and due to the changes in the marketplace I have decided to make a complete transition to programming mobile apps. I have learned a little bit of Objective-C by adapting certain PhoneGap plugins for iPhone to suit the needs of my iPhone app. I hope this blog will be helpful to those looking for information to get started programming mobile apps.

Chris