应用缓存 iOS PhoneGap [英] App cache iOS PhoneGap

查看:26
本文介绍了应用缓存 iOS PhoneGap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在安卓上使用

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

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

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.

如何在 iOS 上实现这一点?

我试过

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

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

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)

谢谢

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

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

和特点:

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

我在 Android 中成功下载了图像:

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:
" + error.source +" 
||| "+error.target+" 
||| "+error.code); 
            }  
        );

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

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.

代替使用

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

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

to download files on iOS, I used just plain

cdvfile://localhost/persistent/ 

我得到的

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

这将我的文件成功下载到

This downloaded my file successfully to

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

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

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

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

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

  • Android 上:

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

我必须添加

Android/com.my.appid/cache/

手动,如果我删除了应用程序,文件也会被删除,这是好的.

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

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.

这篇关于应用缓存 iOS PhoneGap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆