Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Works with cordova-plugin-ionic-webview? #2

Open
remisture opened this issue Nov 6, 2017 · 4 comments
Open

Works with cordova-plugin-ionic-webview? #2

remisture opened this issue Nov 6, 2017 · 4 comments

Comments

@remisture
Copy link

Hi,

Great plugin.

Will it also work with this version: https://github.com/ionic-team/cordova-plugin-ionic-webview ?

@paulpatarinski
Copy link

Check out the https://github.com/paulpatarinski/cordova-plugin-migrate-localstorage fork for ionic-webview

@hellkith
Copy link

@paulpatarinski I've tried your fork of this plugin with ionic-webview 1.1.16 (as pointed out in your disclaimer) however it does not copy the old localStorage (from the UIWebView) to the WKWebView.

I do see in XCode the log that it sees there's no localStorage (as the NSLog of "No existing localstorage data found for WKWebView. Migrating data from UIWebView" is posted), so it attempts to copy over, but then when I check the localStorage it did not actually copy over.
Any suggestions what could be the issue?

@rushabhmakwana-multidots

@hellkith Did you get any solution for coping the old localStoage to the WKWEbView for iOS?

@hellkith
Copy link

yes, I changed some of the code in the MigrateLocalStorage.m.
Below is my full file with what it does. I had to add several extra copies as it didn't copy the correct files at all.

IF you replace the contents of your MigrateLocalStorage.m it should work.
Do note that it will only work if there is no WKWebView yet.
Seeing it shouldn't be copying over it again once there is a WKWebview as it will place back the old storage or delete the current storage.

So easy way to test this is to have an install of your app with the UIWebView, make a backup of that via XCode (Window -> devices -> click on app -> cogwheel -> download container). This way you can always replace the container with the UIWebView again if it fails.
Then run the update on the app so that it upgrades to the WKWebView and it should have copied it over.

`#import "MigrateLocalStorage.h"

@implementation MigrateLocalStorage

  • (BOOL) copyFrom:(NSString*)src to:(NSString*)dest
    {
    NSFileManager* fileManager = [NSFileManager defaultManager];

// Bail out if source file does not exist
if (![fileManager fileExistsAtPath:src]) {
return NO;
}

// Bail out if dest file exists
if ([fileManager fileExistsAtPath:dest]) {
    return NO;
}

// create path to dest
if (![fileManager createDirectoryAtPath:[dest stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil]) {
    return NO;
}

// copy src to dest
return [fileManager copyItemAtPath:src toPath:dest error:nil];

}

  • (void) migrateLocalStorage
    {
    // Migrate UIWebView local storage files to WKWebView. Adapted from
    // https://github.com/Telerik-Verified-Plugins/WKWebView/blob/master/src/ios/MyMainViewController.m

    NSString* appLibraryFolder = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* original;

    if ([[NSFileManager defaultManager] fileExistsAtPath:[appLibraryFolder stringByAppendingPathComponent:@"WebKit/LocalStorage/file__0.localstorage"]]) {
    original = [appLibraryFolder stringByAppendingPathComponent:@"WebKit/LocalStorage"];
    } else {
    original = [appLibraryFolder stringByAppendingPathComponent:@"Caches"];
    }

    NSString* originalFile = [appLibraryFolder stringByAppendingPathComponent: @"WebKit/LocalStorage/file__0.localstorage"];
    NSString* originalFileTarget = [appLibraryFolder stringByAppendingPathComponent:@"Caches/file__0.localstorage"];
    NSString* originalFileshm = [appLibraryFolder stringByAppendingPathComponent:@"WebKit/LocalStorage/file__0.localstorage-shm"];
    NSString* originalFileTargetshm = [appLibraryFolder stringByAppendingPathComponent:@"Caches/file__0.localstorage-shm"];
    NSString* originalFilewal = [appLibraryFolder stringByAppendingPathComponent:@"WebKit/LocalStorage/file__0.localstorage-wal"];
    NSString* originalFileTargetwal = [appLibraryFolder stringByAppendingPathComponent:@"Caches/file__0.localstorage-wal"];

    original = [original stringByAppendingPathComponent:@"file__0.localstorage"];

    NSString* target = [[NSString alloc] initWithString: [appLibraryFolder stringByAppendingPathComponent:@"WebKit"]];

#if TARGET_IPHONE_SIMULATOR
// the simulutor squeezes the bundle id into the path
NSString* bundleIdentifier = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
target = [target stringByAppendingPathComponent:bundleIdentifier];
#endif

target = [target stringByAppendingPathComponent:@"WebsiteData/LocalStorage/http_localhost_8080.localstorage"];

[self copyFrom:originalFile to:originalFileTarget];
[self copyFrom:originalFileshm to:originalFileTargetshm];
[self copyFrom:originalFilewal to:originalFileTargetwal];


// Only copy data if no existing localstorage data exists yet for wkwebview
if (![[NSFileManager defaultManager] fileExistsAtPath:target]) {
    NSLog(@"No existing localstorage data found for WKWebView. Migrating data from UIWebView");
    [self copyFrom:original to:target];
    [self copyFrom:[original stringByAppendingString:@"-shm"] to:[target stringByAppendingString:@"-shm"]];
    [self copyFrom:[original stringByAppendingString:@"-wal"] to:[target stringByAppendingString:@"-wal"]];
}

}

  • (void)pluginInitialize
    {
    [self migrateLocalStorage];
    }

@end`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants