apache / cordova-ios

Apache Cordova iOS
https://cordova.apache.org/
Apache License 2.0
2.16k stars 990 forks source link

Migration of local storage #906

Closed seamlink-aalves closed 4 years ago

seamlink-aalves commented 4 years ago

I've started testing cordova-ios 6.0.0 and I haven't found a way to migrate local storage and indexDB data.

In version 5.1.1 of cordova-ios I was using cordova-plugin-wkwebview-engine and cordova-plugin-wkwebview-file-xhr which I removed when upgrading to 6.0.0. But with the introduction of scheme and hostname preferences to solve the CORS issue I lose previous localstorage and IndexDB.

Is anyone facing this issue or found a way to solve this?

jcesarmobile commented 4 years ago

We don't plan to migrate user's data, that's up to you if you want to migrate it or not.

You can keep using file protocol if you don't configure the scheme and your data will still be there (if you were already using a WKWebView plugin). If you set the scheme then data won't be accessible as the url changes (not lost, the data is still there).

Here are some plugins that do data migration (the linked plugin is one of them, but included other plugins in which its based that can also help you migrating the data) https://github.com/kassamina/cordova-plugin-ionic-migrate-storage#thanks

gerhardsletten commented 4 years ago

@jcesarmobile

You can keep using file protocol if you don't configure the scheme and your data will still be there. If you set the scheme then data won't be accessible as the url changes (not lost, the data is still there).

For a regular cordova app, this is not correct, at least for localstorage. After what I can see cordova-ios-6.x will create new files to store localstorage, I inspected the filesystem for the simulator.

The original name of the localstorage-file seems to be WebKit/LocalStorage/file__0.localstorage, while the new name will depends on the scheme and hostname set in config.xm, so if you set

<preference name="scheme" value="app" />
<preference name="hostname" value="localhost" />

the new location will be WebKit/WebsiteData/LocalStorage/app_localhost_0.localstorage

To migrate this none of the plugins mentions here worked out of the box: https://github.com/kassamina/cordova-plugin-ionic-migrate-storage#thanks

But I used https://github.com/jairemix/cordova-plugin-migrate-localstorage and updated the new location of localstorage based on my findings while inspection this. The tip is to uncomment the NSLog which logs file-location, and then in terminal on your mac open those in finder with the open command:

$ open /User/xx/...

You will then be able to find those files and adjust the files after your need. Mine was to change this line

#define TARGET_LS_FILEPATH @"WebKit/WebsiteData/LocalStorage/http_localhost_8080.localstorage"

to this:

#define TARGET_LS_FILEPATH @"WebKit/WebsiteData/LocalStorage/app_localhost_0.localstorage"
jcesarmobile commented 4 years ago

I don't see how what you say conflicts whit what I said.

I say, if you were using cordova-plugin-wkwebview-engine plugin and move to cordova-ios 6, the data will still be there if you use file protocol. If you configure the app to use a scheme and hostname, then the data won't be accessible from the app (but not lost).

If you were using UIWebView, then yeah, the data won't be accessible even if using file protocol because they are different WebViews. If migrating from UIWebView to WKWebView or moving from file to a custom scheme, users that want to migrate their data will need to use some plugin like the one you mentioned.

gerhardsletten commented 4 years ago

@jcesarmobile yes, I ment migration from cordova-ios-5.x and UIWebView directly to cordova-ios-6 and WKWebView, so then as you say, localstorage will not be migrated for you and you will need to fix this yourself.

jcesarmobile commented 4 years ago

Updated my original comment to make clear it only applies if already using WKWebView as the reporter was doing.

SuneRadich commented 3 years ago

@jcesarmobile

You can keep using file protocol if you don't configure the scheme and your data will still be there. If you set the scheme then data won't be accessible as the url changes (not lost, the data is still there).

For a regular cordova app, this is not correct, at least for localstorage. After what I can see cordova-ios-6.x will create new files to store localstorage, I inspected the filesystem for the simulator.

The original name of the localstorage-file seems to be WebKit/LocalStorage/file__0.localstorage, while the new name will depends on the scheme and hostname set in config.xm, so if you set

<preference name="scheme" value="app" />
<preference name="hostname" value="localhost" />

the new location will be WebKit/WebsiteData/LocalStorage/app_localhost_0.localstorage

To migrate this none of the plugins mentions here worked out of the box: https://github.com/kassamina/cordova-plugin-ionic-migrate-storage#thanks

But I used https://github.com/jairemix/cordova-plugin-migrate-localstorage and updated the new location of localstorage based on my findings while inspection this. The tip is to uncomment the NSLog which logs file-location, and then in terminal on your mac open those in finder with the open command:

$ open /User/xx/...

You will then be able to find those files and adjust the files after your need. Mine was to change this line

#define TARGET_LS_FILEPATH @"WebKit/WebsiteData/LocalStorage/http_localhost_8080.localstorage"

to this:

#define TARGET_LS_FILEPATH @"WebKit/WebsiteData/LocalStorage/app_localhost_0.localstorage"

Not wanting to ressurrect an old issue, but I just wanted to say that following the above overall guide, I was able to migrate localStorage from UIWebView to WKWebView when we updated to the latest platform-ios.

mushishi78 commented 3 years ago

I also appreciated the above comment from @gerhardsletten. I ended up needing more changes to get it to work for me, so my fork is here if it helps anyone: https://github.com/mushishi78/cordova-plugin-migrate-localstorage

Viglino commented 3 years ago

I've implemented @gerhardsletten solution to use with regular cordova app ios-6.x with app scheme here: https://github.com/Viglino/cordova-plugin-migrate-localstorage

qz-syagawa commented 1 year ago

I also appreciated the above comment from @gerhardsletten. I ended up needing more changes to get it to work for me, so my fork is here if it helps anyone: https://github.com/mushishi78/cordova-plugin-migrate-localstorage

I had a problem with localStorage when migrating from cordova-ios@5.01 to 7.0.0. I solve it with https://github.com/mushishi78/cordova-plugin-migrate-localstorage . thank you!!