Saved images not showing after restart

Hi, in our app we save many images to the device, after moving to 2.8 I am not noticing that after restarting the app, the images are no longer there, we just get a broken image reference.

We are using a set port for httpd as described in the docs and the localstorage seems to work indicating that the port is set, however the images are not there.

We save it like this

forge.file.saveURL(url, function (file) {
    fileObject = JSON.stringify(forge.httpd.normalize(file));
    localStorage.setItem('topLogo', fileObject);

Then store the file object in a localstorage and use that to reference it in our output. As noted, this works the first time however if you close and reopen the app it cant find the images

forge.httpd.normalize() should no longer be required with platform version v2.8.x

I’ve tested that it works with the following code:

var url = "https://trigger.io/forge-static/img/trigger-t.png";

// save it
forge.file.saveURL(url, function (file) {
    var fileObject = JSON.stringify(file);
    localStorage.setItem("test-image", fileObject);
});

// load it
var fileObject = localStorage.getItem("test-image");
forge.logging.log("localStorage.getItem -> " + fileObject);
var file = JSON.parse(fileObject);
forge.file.URL(file, function (url) {
    forge.logging.log("file.URL -> " + url);
    $("#test-image").attr("src", url);
});

What are you actually getting back from localStorage?

If you’re getting null then there’s probably a problem with the httpd configuration where a fixed port isn’t being set leading to localStorage creating a new store every time the app is restarted.

A working configuration for v2.8.x with a fixed port goes something like:

  1. remove the httpd module if it’s still in your config
  2. In your src/config.json add the following to the core.general section:
"core": {
    "general": {
        "httpd": {
            "port": 12345
        }
    }
} 

Note that a forge build ios && forge run ios will often delete any previously downloaded data.

Finally, maybe also check whether you have any flags set that could be messing with localStorage.

Hi Antoine,

This seems to have fixed the problem ( removeing httpd and normalize URLs )

One question though, is the removal of photos and images unique to the forge build or will it happen on app updates from IOS as well?

I have experianced this in the past and have built in some checking code where I check to see if a file can be accessed and if not I redownload however this code no longer seems to fire… Its very odd as the app seems to ignore basic JS code in the index, so a simple alert never seems to fire nor does my checking code.

I’m still trying to get to the bottom of why this would even happen, its like its cached in some way and not honoring updated js or html after the fore install ( i’m going direct to phone )

Just to add some things, when I try and run once the build has already been installed, aside from the images not being there and my scripts not running it comes through on the debug as

Kill() returned unexpected error 1

Not sure what’s going on if it’s the entries in LocalStorage are going missing. LocalStorage is part of Apple’s underlying WKWebView code so it’s not something we have access to.

If the files themselves are no longer there it’s more complex, on iOS there are three options for file storage:

  1. Temporary files that are stored in ~/tmp/ or ~/Library/Caches: Not guaranteed to persist between app launches. (Although they often do) forge.file.cacheURL saves files there.

  2. User-generated files/content that are store in ~/Documents/: The closest to a persistent storage option. Can also be backed up to iCloud. However, files saved here will show up in the “Files” app and sometimes in the various share menus. Apple can also get irate if you try store anything there which isn’t obviously user-generated. Therefore we cannot use this for the forge.file API’s

  3. Files which are not temporary files or user-generated data files that are stored in ~/Library/Application Support: Apple imply these won’t be garbage collected but they don’t say so explicitly. This is where forge.file.saveURL saves files.

Does your code wait for the page to finish loading?

i.e.

document.addEventListener("DOMContentLoaded", function(event) { 
    // code to execute once page has finished loading
});

or using jQuery:

$(document).ready(function () 
    // code to execute once page has finished loading
});

Finally, you asked:

It’s a WebKit bug, a fix has been pushed upstream but we’re still waiting for Apple to release it. You can safely ignore it.

More info: https://stackoverflow.com/questions/58321114/