1

I am using Apple On-Demand resources to try to load a game inside my App.

The ODR retrieval works well (with a custom plugin that I developed) but then I am able to read the downloaded on-demand resource only on the simulator, I cannot do the same on a physical device!

The path of the downloaded file that I get through the Objective-C code of my custom plugin
( NSString *path= [[NSBundle mainBundle] pathForResource:@"flying-block-game" ofType:@"html" ]; )
..is the following:

  • Simulator

[odr] Path for flying-block-game.html: '/Users/myUser/Library/Developer/CoreSimulator/Devices/D016D3BC-E9F9-43F2-8EC1-9A471819A9B5/data/Library/OnDemandResources/AssetPacks/89A6F4A7-D148-419B-B5BB-FF23950612E2/15538308173473852725/com.us-company.sports-beta-enterprise.asset-pack-5a105e8b9d40e1329780d62ea2265d8a.assetpack/flying-block-game.html'

  • Physical device:

[odr] Path for flying-block-game.html: '/var/mobile/Library/OnDemandResources/AssetPacks/2F2887A0-7A16-4E5D-81C2-1C688A69DA80/15538308173473852725/com.us-company.sports-beta-enterprise.asset-pack-5a105e8b9d40e1329780d62ea2265d8a.assetpack/flying-block-game.html'


The error I get when I try to read the file (using cordova-plugin-file) is 5 - ENCODING_ERR but I doubt it's accurate as I also got the same trying to fetch files from a non-existing path.

This is the JS code I use to try to read the downloaded on-demand asset using the path returned by my custom plugin:

// On-Demand Resources POC
  window.plugins.fetchOdr({ // my custom plugin
    tagName: 'my-test-tag',
    success: (path) => {
      alert(`Success! Odr path: ${path}`);
      // This Breaks.. v V V vvvvvvvVVVVVVvvvvvVVVVvv o . <<------------------------=
      window.resolveLocalFileSystemURL(
        `file://${path}`,
        (data) => {
          const fileEntry = data;
          fileEntry.file(
            (file) => {
              const reader = new FileReader();
              reader.onloadend = function(data) {
                console.log(`[odr] Successful file read: ${this.result}`, data);
                // do stuff here.. (display in webview)
              };
              reader.readAsText(file);
            },
            (err) => alert(`[odr] > File read Error: ${JSON.stringify(err)}`),
          );
        },
        (err) => alert(`[odr] > FileEntry generation Error: ${JSON.stringify(err)}`),
      );
    },


What am I doing wrong? How can I properly read the file on the physical device too?


I even added these preferences to my config.xml file but it didn't help:

<preference id="TEMPORARY-FULL-WHITELIST" name="iosExtraFilesystems" value="library,library-nosync,documents,documents-nosync,cache,bundle,root" />
<preference id="ODR-POC" name="iosPersistentFileLocation" value="Library" />
Gabe
  • 5,997
  • 5
  • 46
  • 92
  • 1
    Update: I opted for a different solution. I simply read and return the contents of the file from the Objective-C code using this: `NSString* content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];` – Gabe Apr 08 '20 at 07:15
  • 1
    You download into the app bundle? I doubt it. That's a major difference between simulator and device though; on the simulator you can add files to the bundle at runtime without complaint and on the device you cannot. The bundle is signed for security and adding files to it breaks its integrity. You should be downloading stuff into a [temp folder](https://stackoverflow.com/questions/11897825/ios-temporary-folder-location). – trojanfoe Apr 08 '20 at 07:16
  • Having said all that, your call to `[NSBundle pathForResource:ofType:]` doesn't appear to be pointing into your app bundle. Is there some sort of auto-routing going on? Anyway it looks like you are doing the right thing already... – trojanfoe Apr 08 '20 at 07:24
  • Well as you suggested yes it's looking into my app bundle: `NSBundle mainBundle`. I got it working with the stringWithContentsOfFile call. Not entirely sure but this may have worked (including /private): "file://private/var/mobile/Library/..." – Gabe Apr 08 '20 at 07:30

0 Answers0