LoginSignup
2
2

More than 5 years have passed since last update.

PhoneGap/Cordova ダウンロードしたファイルをiCloudバックアップ対象外にする

Last updated at Posted at 2016-02-17

概要

PhoneGapでサーバーから画像・音声・動画をダウンロードして表示・再生までで書いていた方法で動画などをダウンロードさせるアプリをAppStore申請したらリジェクトされました。

(一部抜粋)

On launch and content download, your app stores over 25 MB on the user's
iCloud, which does not comply with the iOS Data Storage Guidelines.

Next Steps

Please verify that only the content that the user creates using your app, e.g.,
documents, new files, edits, etc. is backed up by iCloud as required by the iOS
Data Storage Guidelines. Also, check that any temporary files used by your app
are only stored in the /tmp directory; please remember to remove or delete the
files stored in this location when it is determined they are no longer needed.

Data that can be recreated but must persist for proper functioning of your app -
or because users expect it to be available for offline use - should be marked
with the "do not back up" attribute. For NSURL objects, add the
NSURLIsExcludedFromBackupKey attribute to prevent the corresponding file from
being backed up. For CFURLRef objects, use the corresponding
kCRUFLIsExcludedFromBackupKey attribute.

Resources

To check how much data your app is storing:

  • Install and launch your app
  • Go to Settings > iCloud > Storage > Manage Storage
  • Select your device
  • If necessary, tap "Show all apps"
  • Check your app's storage

For additional information on preventing files from being backed up to
iCloud and iTunes, see Technical Q&A 1719: How do I prevent files from
being backed up to iCloud and iTunes.

要するに、iCloudにバックアップ対象となるファイルは、25MBを超えるなと…

方針

/tmp フォルダにいれればバックアップされないらしいが、
https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
ここを読むと、フォルダのファイルは、勝手にOSに消されることがあるらしい。今回はそれでは困るので、もとのフォルダ構造のままの解決法をさぐる。

PhoneGap/Cordovaのconfig.xml設定を変えるが、意味ない。

https://cordova.apache.org/docs/en/latest/guide/platforms/ios/config.html
ここにあるとおり、config.xmlに、

config.xml
<preference name="BackupWebStorage" value="none"/>

と書いてみる。バックアップ容量変わらず、意味なかった。
もしかしたら、ダウンロードファイル以外のデータ(localStorageの保存データなど)は意味があるのかもしれないが、細かい切り分けまでできていない。
とにかく、File-Transferでダウンロードしたファイルは意味ない。

解決策

Stack Overflow などを渡り歩いていたら、ファイルのメタデータを変更する方法にたどり着く。
https://cordova.apache.org/docs/en/3.0.0/cordova/file/fileentry/fileentry.html
を見ると、iOSだけ FileEntry.setMetadata() というメソッドが使え、com.apple.MobileBackup1 にすれば、バックアップされない(0にするとバックアップされる)とのこと。

結果としては、これで上手くいきました。

コード

別記事の一部抜粋します。

function dl_img(){
    alert('画像のダウンロードを開始するよ');
    var fileTransfer = new FileTransfer();
    //ダウンロードするURL
    var url = encodeURI('https://secure.elephancube.jp/dltestapp/120305.png');
    //保存するパス
    var filePath = rootDir + 'testapp/test.png';
    fileTransfer.download(url, filePath, function(entry) {
        alert('ダウンロード成功 '+filePath);
        //★★★ここでメタデータをセットする★★★
        entry.setMetadata(function(){
            //success
        },function(){
            //fail
        },
        {
            "com.apple.MobileBackup": 1
        });
        //★★★ここまで★★★
        //表示
        $('#view').html('<img src="'+filePath+'" width="100%">');
    }, function(error) {
        alert('ダウンロードエラー '+error.code);
    });
}

★★★で囲まれた部分が追加部分です。
ダウンロード成功時のFileEntryオブジェクトにsetMetadataします。

なかなかハマりました。以上。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2