应用程序缓存的iOS的PhoneGap缓存、应用程序、PhoneGap、iOS

2023-09-05 03:08:14 作者:断桥上的等待◇

在Android上我用

 文件:///storage/sdcard0/Android/data/my.app.id/cache/
 

下载(文件传输)的一些图像和然后告诉他们在我的HTML。图像和其他一切该文件夹中删除我的应用程序,这正是我想要的时候被删除。

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

我试着用

 文件:///无功/移动/应用/< GUID的应用程序的制造> /文件/
 
基于PhoneGap的iOS平台入门教程

因为这是我的请求时,文件系统,但图像甚至不会下载(误差code 1)获得。有任何想法吗?我发现更多关于我的错误:它是作为this题。 (无法创建路径保存下载文件 - 可可错误512)

感谢

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

 <差距:插件名称=org.apache.cordova.file/>
<差距:插件名称=org.apache.cordova.file转移/>
 

和特点:

 <功能名称=文件>
    < PARAM NAME =Android的包值=org.apache.cordova.file.FileUtils/>
< /功能>
<功能名称=文件>
    < PARAM NAME =IOS-包值=CDVFile/>
< /功能>
<功能名称=文件传输>
    < PARAM NAME =IOS-包值=CDVFileTransfer/>
< /功能>
 

我在Android的成功下载图像:

  VAR文件传输=新的文件传输();
VAR URI = EN codeURI(myserverurl /+文件名);
VAR文件路径= appCachePath +文件名;
        fileTransfer.download(
            URI,
            文件路径,
            功能(输入){
                警报(下载完成:+ entry.fullPath);
            },
            功能(错误){
                警报(下载错误源/目标/ code:\ N+ error.source +\ñ|||+ error.target +\ñ|||+错误code);
            }
        );
 

我将它们下载后,我顺利拿到FS与

 函数onDeviceReady(){
    window.requestFileSystem(LocalFileSystem.PERSISTENT,0,gotFS,失败);
    如果(device.platform ===安卓){
        cacheFolderSubPath =安卓/数据/ id.app.my /缓存/;
    }
    其他{
        cacheFolderSubPath =; //为iOS是什么?
    }
}

功能gotFS(文件系统){
    变种nturl = fileSystem.root.toNativeURL(); //返回cdvfile://本地主机/永久/为iOS和Android
    window.resolveLocalFileSystemURL(nturl + cacheFolderSubPath,onResolveSuccess,onResolveFail);
}

功能onResolveSuccess(fileEntry){
     //这是文件:/// ......在上面的问题中提到的字符串。
    appCachePath = fileEntry.toNativeURL()+/;
    //等等等等等等...(使用appCachePath与下载code以上)
}
 

解决方案

在此我发现不是那么明显,但很容易的解决方案挣扎的日子。

而不是使用

 文件:///无功/移动/应用/< GUID的应用程序的制造> /文件/
 

下载iOS上的文件,我用的只是普通的

  cdvfile://本地主机/永久/
 

这是我从了

  fileSystem.root.toNativeURL(); //同样为Android和iOS
 

这我下载的文件成功地

 文件:///无功/移动/应用/< GUID的应用程序的制造> /文件/
 

这也是我用来在HTML中显示图像的SRC路径。

我想弄清楚发生了什么事在我的情况:的(使用resolveLocalFileSystemURL时,())

在机器人

  cdvfile://本地主机/永久/  - >文件:///存储/ sdcard0 /
 

我不得不添加

 安卓/ com.my.appid /缓存/
 

手动,如果我删除了应用程序,文件已经删除,以及它是 确定。

在的iOS

  cdvfile://本地主机/永久/  - >文件:///无功/移动/应用/< GUID的应用程序的制造> /文件/
 

在这里,我并不需要添加任何东西,持久化存储已经 指着我的应用程序本身的缓存,这是在这种情况下,文档 夹。这种获取与应用程序,以及它是正确的去除。

On Android I used

file:///storage/sdcard0/Android/data/my.app.id/cache/

to download (FileTransfer) some images and then show them in my html. Images and everything else inside this folder gets removed when deleting my app, which is exactly what I want.

How to achieve this on iOS?

I tried with

file:///var/mobile/Applications/<GUID of app>/Documents/

since this is what I got when requesting file system, but the images won't even download (error code 1). Any ideas? I found out more about my error: it's the same error as stated in this question. (Could not create path to save downloaded file - Cocoa Error 512)

Thanks

More info: Relevant plugins I use are

<gap:plugin name="org.apache.cordova.file" />
<gap:plugin name="org.apache.cordova.file-transfer" />

and features:

<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>

I download images successfully in Android with:

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); 
            }  
        );

I download them AFTER I successfully get FS with

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)
}

解决方案

After days of struggling with this I found the not-so-obvious but very easy solution.

Instead of using

file:///var/mobile/Applications/<GUID of app>/Documents/

to download files on iOS, I used just plain

cdvfile://localhost/persistent/ 

that I got from

fileSystem.root.toNativeURL(); // same for Android and iOS

This downloaded my file successfully to

file:///var/mobile/Applications/<GUID of app>/Documents/

and this is also the src path I used to display images in HTML.

Just to clarify what happened in my case: ( when using resolveLocalFileSystemURL() )

On Android:

cdvfile://localhost/persistent/ -> file:///storage/sdcard0/

I had to add

Android/com.my.appid/cache/

manually and if I removed the app, files got removed as well which is OK.

On iOS:

cdvfile://localhost/persistent/ -> file:///var/mobile/Applications/<GUID of app>/Documents/

Here I didn't need to add anything, persistent storage was already pointing to my app's own 'cache' which is in this case in Documents folder. This gets removed with the app as well which is correct.