0

I am facing issue with localstorage in iphone/Ipad with os version 10 and above.

The scenario is like this.

First time When i open the application the localstorage key value is null . At that time i'm assigning value to that corresponding key.

second time When i open the application still localStorage key is null again assigning value to that corresponding key.

Third time when i open the application i'm getting value from localStorage . If i repeat above steps no.of time , everytime i'm able to get value from localstorage.

Here the problem is second time only with localStorage.

Below are the technologies that i'm using IBM Mobile First V7.1 Cordova V3.7 Ionic V1.2.4 and angular 1.4

Please find below sample code that is working fine in iphone & ipad with os 9(below 10)

if(localStorage && $window.localStorage.getItem("isFirst") ==  "first"){ 
  alert("Data available"); 

}else{ 
 alert("LocalStorage is empty");               
$window.localStorage.setItem("isFirst", "first"); 

}

got "Data available" alet twice instead of one.

Vittal Pai
  • 2,725
  • 18
  • 28

1 Answers1

2

LocalStorage on iOS has been known to be not predictable. Some LocalStorage find erased when restarting the app (without Cordova or MFP even), and some do not. iOS may erase LocalStorage when the device is running low on memory. But this isn't specific to iOS 10.

This is not an issue with MFP or Cordova, it probably is just how iOS works.

To persist data reliably and with encryption you can use JSONStore. More details here:

1) JSONStore Tutorial

2) Overview

Vivin K
  • 2,597
  • 1
  • 9
  • 14
  • We are facing this only in ios 10, verified no.of times in below os 10 didn't reproduce . – Ravali Abboju Mar 31 '17 at 08:37
  • if the issue with "low on memory" in my case localstorage is writing after second attempt which should not happen right?? – Ravali Abboju Mar 31 '17 at 08:39
  • Low on memory was an example on when it may get cleared. You are dealing with HTML5 storage and this is not handled by MFP or Cordova. The behaviour is dependent on the Webview/HTML5 engine and cannot be controlled in your / MFP code. If you want persistence then use JSONStore. – Vivin K Mar 31 '17 at 11:30
  • We have tried it, but intialization of JSONStore collection is taking time while loading the application – Ravali Abboju Apr 04 '17 at 05:23
  • @RavaliAbboju, this is a different discussion, jsonstore initialization should be after the application is laoded. And how big is your collection ? – Vivin K Apr 04 '17 at 06:51