Can HTML5 localStorage in Cordova/Phonegap app be

2019-04-14 06:30发布

问题:

I have a Phonegap/Cordova app that runs on iOS. It saves it's data into HTML5 localStorage.

I'm trying to work out if it's possible to sync the localStorage data (using iCloud) to other iOS devices, and even OS X.

From what I can see, in iOS localStorage is actually implemented as a SQLite database, which (when using Phonegap/Cordova) is written to the app's Documents directory:

Documents/Backups/localstorage.appdata.db

I also understand that there are three main ways of storing data in iCloud:

  • Key/Value storage
  • UIDocument / NSDocument
  • Core Data

I know I can't use the Key/Value iCloud storage method, because I have more than 1MB of data to store, and the limitation is 1MB per app with that method.

This question, I believe is talking about the UIDocument method, and asks if it is possible to store a SQLite db file in iCloud using that method. The answer is no because the database may become corrupted.

So that really leaves the Core Data method.

So my question is - would this work? Could I sync the localStorage.db file to iCloud using Core Data?

I've never used Core Data and don't know much about it. I'm just wondering if it would be possible, or if there is something else I don't understand.

Are there any other ways to sync localStorage data between iOS devices or OS X ?

回答1:

The answer unfortunatly appears to be no, Core Data cannot be used with HTML5 localStorage

Core Data can not be used with SQLite databases other than ones created with Core Data. If you try to, you get this error in XCode:

SQLite error code:1, 'no such table: Z_METADATA'

This is explained the Core Data docs:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdFAQ.html#//apple_ref/doc/uid/TP40001802-SW2

Although Core Data supports SQLite as one of its persistent store types, the database format is private. You cannot create a SQLite database using native SQLite API and use it directly with Core Data (nor should you manipulate an existing Core Data SQLite store using native SQLite API)

I still want to solve this issue though. I'm thinking of creating a Javascript API that mirrors the localStorage API. This would be a phonegap plugin that can call objective-c code, and effectively write it's changes to a Core Data database. The Core Data database should then be able to be synced to iCloud.

If it works, I'll come back and update this answer.