blog, Mobile

Cordova App and localStorage

Continuing on with my hatred of developing a hybrid app . . .

I had tried several login options and was having a difficult time getting any to work.  After trying to get stuff to work about 3 different times I found a good tutorial at  Walking through the example line by line was still giving me some errors.  I figured that $5 was a small price to pay rather than spend hours trying to debug my efforts.  I got the code and . . . it didn’t work.  But that was because of the configuration of my server.  I had to change the mysql calls to the updated mysqli.  But that wasn’t a big deal.

This login system didn’t give me exactly what I needed for the hybrid app I was creating.  But the logic gave me a good idea on where some of my errors were.  So I re-wrote another login system and it was working.

For ease of usage there was some data that was being stored into localStorage on the device.  So the app needed a way to check to see if there was local storage on the device and if not redirect the user to the login page.  This is another issue where I found that localStorage on a mobile phone is a bit different than in a web browser.  The jQuery code that worked cross browser did not work on the phone app.

At this point I scoured the web looking for code examples that worked.  Testing code samples again and again noting appeared to be working.   Also, once again, was worthless giving me lots and lots of worthless non-working code.

There were 3 items the app was setting in localStorage: session id, store number, and user number.  After running through a bunch of tests the final code seemed to simple to be true:

  $(function(checkLocalStorage) {
    if (localStorage.getItem('sessionID')!==null 
        && localStorage.getItem('storeNum')!==null 
        && localStorage.getItem('userNum')!==null) {

This piece of code may not be perfect and needs a little tweaking, but it works.  And it works better than anything that I found searching the Internet.