应用缓存iOS PhoneGap

tra*_*sis 6 android ios cordova phonegap-build

在我使用的Android上

file:///storage/sdcard0/Android/data/my.app.id/cache/
Run Code Online (Sandbox Code Playgroud)

下载(FileTransfer)一些图像,然后在我的HTML中显示它们.删除我的应用程序时,图像和此文件夹中的所有内容都将被删除,这正是我想要的.

如何在iOS上实现这一目标?

我试过了

file:///var/mobile/Applications/<GUID of app>/Documents/
Run Code Online (Sandbox Code Playgroud)

因为这是我在请求文件系统时得到的,但图像甚至不会下载(错误代码1).有任何想法吗?我发现了更多关于我的错误:这是与问题中所述相同的错误.(无法创建保存下载文件的路径 - Cocoa Error 512)

谢谢


更多信息: 我使用的相关插件是

<gap:plugin name="org.apache.cordova.file" />
<gap:plugin name="org.apache.cordova.file-transfer" />
Run Code Online (Sandbox Code Playgroud)

和功能:

<feature name="File">
    <param name="android-package" value="org.apache.cordova.file.FileUtils" />
</feature>
<feature name="File">
    <param name="ios-package" value="CDVFile" />
</feature>
<feature name="FileTransfer">
    <param name="ios-package" value="CDVFileTransfer" />
</feature>
Run Code Online (Sandbox Code Playgroud)

我在Android中成功下载图片:

var fileTransfer = new FileTransfer();    
var uri = encodeURI("myserverurl/"+fileName); 
var filePath = appCachePath+fileName;  
        fileTransfer.download(
            uri,
            filePath,
            function(entry) {
                alert("download complete: " + entry.fullPath); 
            },
            function(error) {  
                alert("download error source/target/code:\n" + error.source +" \n||| "+error.target+" \n||| "+error.code); 
            }  
        );
Run Code Online (Sandbox Code Playgroud)

我成功获得FS之后下载它们

function onDeviceReady() { 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
    if(device.platform === 'Android'){
        cacheFolderSubPath = "Android/data/id.app.my/cache/";  
    }
    else{
        cacheFolderSubPath =  ""; //  for iOS what ??   
    }  
}

function gotFS(fileSystem) {    
    var nturl = fileSystem.root.toNativeURL();  // returns cdvfile://localhost/persistent/ for both iOS and Android 
    window.resolveLocalFileSystemURL(nturl+cacheFolderSubPath, onResolveSuccess, onResolveFail);  
}

function onResolveSuccess(fileEntry) { 
     // this is "file:///..." string mentioned in the question above.
    appCachePath = fileEntry.toNativeURL()+"/";  
    // etc etc etc ... (use this appCachePath with download code above)
}
Run Code Online (Sandbox Code Playgroud)

tra*_*sis 3

经过几天的努力,我找到了一个不太明显但非常简单的解决方案。

而不是使用

file:///var/mobile/Applications/<GUID of app>/Documents/
Run Code Online (Sandbox Code Playgroud)

要在 iOS 上下载文件,我只使用了普通的

cdvfile://localhost/persistent/ 
Run Code Online (Sandbox Code Playgroud)

我从

fileSystem.root.toNativeURL(); // same for Android and iOS
Run Code Online (Sandbox Code Playgroud)

这将我的文件成功下载到

file:///var/mobile/Applications/<GUID of app>/Documents/
Run Code Online (Sandbox Code Playgroud)

这也是我用来显示 HTML 图像的 src 路径。


只是为了澄清我的情况发生了什么:(使用resolveLocalFileSystemURL()时)

  • 在安卓上:

    cdvfile://localhost/persistent/ -> file:///storage/sdcard0/
    
    Run Code Online (Sandbox Code Playgroud)

    我必须添加

    Android/com.my.appid/cache/
    
    Run Code Online (Sandbox Code Playgroud)

    手动,如果我删除了应用程序,文件也会被删除,这没关系。

  • 在iOS上:

    cdvfile://localhost/persistent/ -> file:///var/mobile/Applications/<GUID of app>/Documents/
    
    Run Code Online (Sandbox Code Playgroud)

    在这里,我不需要添加任何内容,持久存储已经指向我的应用程序自己的“缓存”,在本例中位于“文档”文件夹中。这也被应用程序删除了,这是正确的。