簡體   English   中英

應用緩存iOS PhoneGap

[英]App cache iOS PhoneGap

在我使用的Android上

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

下載( FileTransfer )一些圖像,然后在我的HTML中顯示它們。 刪除我的應用時,圖像和此文件夾中的所有內容都會被刪除,這正是我想要的。

如何在iOS上實現這一目標?

我試過了

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

因為這是我在請求文件系統時得到的,但圖像甚至不會下載(錯誤代碼1)。 有任何想法嗎? 我發現了更多關於我的錯誤:這是與問題中所述相同的錯誤。 (無法創建保存下載文件的路徑 - 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中成功下載圖片:

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

我成功獲得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)
}

經過幾天的努力,我找到了不那么明顯但非常簡單的解決方案。

而不是使用

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

要在iOS上下載文件,我只是簡單地使用了

cdvfile://localhost/persistent/ 

我來自

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

這成功下載了我的文件

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

這也是我用來在HTML中顯示圖像的src路徑。


只是為了澄清我的情況:(使用resolveLocalFileSystemURL()時)

  • Android上

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

    我不得不補充一下

     Android/com.my.appid/cache/ 

    手動,如果我刪除了應用程序,文件也被刪除,這是好的。

  • iOS上

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

    在這里我不需要添加任何東西,持久存儲已經指向我的應用程序自己的'緩存',在本例中是在Documents文件夾中。 這也可以通過應用程序刪除,這是正確的。

錯誤代碼1表示找不到該文件。

您需要獲取存儲應用程序的應用程序目錄。這就是您的文件所在的位置

var imagePath = 'file:///var/mobile/Applications/E50F2661-798B-4F7D-9E6D- BCC266286934/tmp/cdv_photo_011.jpg'

FileErrors = {
    1:   'File not found',
    2:   'Security error',
    3:   'Action Aborted',
    4:   'File not readable',
    5:   'Encoding error',
    6:   'No modification allowed',
    7:   'Invalid state error',
    8:   'Syntax error',
    9:   'Invalid modification error',
    10:   'Quota Exceeded',
    11:   'Type mismatch',
    12:  'Path exists'
};

// extract the application directory from the full path
findApplicationDirectory = function (imagePath) {
  var dir
      , index = fullPath.indexOf('file:///') > -1 ? 6 : 4

  if(fullPath.indexOf('/') > -1){
    fullPath = fullPath.split('/')

    if(fullPath[index]){
      dir = fullPath[index]
    }
  }

  return dir
}

// downloads a file from the server
// @param {string} url
// @param {string} filePath
// @param {function} callback
downloadFile = function (url, fileName, callback){

    getApplicationDirectory(function (applicationDirectory){

        var downloader = new FileTransfer()
                , fileLocation =  findApplicationDirectory(imagePath) + '/' + fileName;

        downloader.download(
            url,
            fileLocation,
            function(entry) {
                console.log("-- downloadFile: download complete: " + entry.fullPath);

                if(typeof callback == 'function'){
                    console.log('-- getFile: calling back');
                    callback.call(this, entry.fullPath);
                }
            },
            function(error) {
                console.log("-- downloadFile: fileLocation " + fileLocation);
                console.log("-- downloadFile: download error source " + error.source);
                console.log("-- downloadFile: download error target " + error.target);
                console.log("-- downloadFile: download error code " + FileErrors[error.code]);
            }
        );
    });
};

通過將UIFileSharingEnabled =“應用程序支持iTunes文件共享”添加到項目plist,確保您可以訪問iOS上的Documents文件夾。 然后,您可以通過在設備上運行應用程序,將其連接到iTunes以及設備列表中找到應用程序的應用程序列表來查看應用程序文檔文件夾的內容。

要將任何文件復制到/ Documents目錄,您可以使用以下內容

// move photo to documents directory
// @param {string} imagePath
// @return {string} newImagePath
// usage savePhotoToDocuments(imagePath).done(function (newImagePath) { }).fail(function () { })
savePhotoToDocuments = function (imagePath){

    function onFileSystemSuccess(fileSystem) {
        console.log('onFileSystemSuccess: fileSystem.name ' + fileSystem.name);

        window.resolveLocalFileSystemURI(directoryPath, onGetDocumentDirectorySuccess, onGetDocumentDirectoryFail)
    }

    function onFileSystemFail(error){
        console.log('onFileSystemFail: ' + FileErrors[error.code])

        promise.reject(error)
    }

    function onResolveSuccess(fileEntry) {
        imageFileEntry = fileEntry

        imageFileEntry.copyTo(documentDirectory, newImageName, onCopyToSuccess, onCopyToFailed)
        console.log('onResolveSuccess: ' + fileEntry);
    }

    function onResolveFail(error) {
        console.log('onResolveFail: ' + FileErrors[error.code]);

        promise.reject(error)
    }

    function onGetDocumentDirectoryFail (error){
        console.log('onGetDocumentDirectoryFail: ' + FileErrors[error.code]);

        promise.reject(error)
    }

    function onGetDocumentDirectorySuccess (directoryEntry){
        documentDirectory = directoryEntry

        console.log('onGetDocumentDirectorySuccess')

        window.resolveLocalFileSystemURI(imagePath, onResolveSuccess, onResolveFail)
    }

    function onCopyToSuccess (fileEntry) {
        console.log('-- savePhotoToDocuments: onCopyToSuccess')

        promise.resolve(newImagePath)
    }

    function onCopyToFailed (error){
        console.log('-- savePhotoToDocuments: onCopyToFailed - ' + FileErrors[error.code])

        // invalid modification error
        // meaning the file already exists
        if(error.code != 9){
            promise.reject(error)
        } else {
            promise.resolve(newImagePath)
        }
    }

    var imageFileEntry
            , documentDirectory
            , promise = $.Deferred()
            //, imagePath = 'file:///var/mobile/Applications/E50F2651-798B-4F7D-9E6D-BCC266286934/tmp/cdv_photo_011.jpg'
            , imageName = imagePath.substring(imagePath.lastIndexOf('/')+1)
            , newImageName = Date.now() + '_' + imageName
            , directoryPath = 'file:///var/mobile/Applications/' + findApplicationDirectory(imagePath) + '/Documents'
            , newImagePath = directoryPath + '/' + newImageName

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onFileSystemFail)

    return promise
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM