@capacitor/filesystem
ファイルシステムAPIは、デバイス上のファイルを操作するためのNodeJSライクなAPIを提供します。
Install
npm install @capacitor/filesystem
npx cap sync
Apple プライバシーマニフェストの要件
Appleは、ユーザーのプライバシーを向上させるために、アプリ開発者がAPI使用の承認された理由を指定することを義務付けています。2024年5月1日までに、App Store Connectにアプリを提出する際にこれらの理由を含める必要があります。
このプラグインをアプリで使用する場合、/ios/AppにPrivacyInfo.xcprivacyファイルを作成するか、VS Code拡張機能を使用して生成し、使用理由を指定する必要があります。
詳細な手順については、Capacitorドキュメントを参照してください。
このプラグインで必要な辞書キーはNSPrivacyAccessedAPICategoryFileTimestampで、推奨される理由はC617.1です。
PrivacyInfo.xcprivacyの例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPrivacyAccessedAPITypes</key>
<array>
<!-- Add this dict entry to the array if the PrivacyInfo file already exists -->
<dict>
<key>NSPrivacyAccessedAPIType</key>
<string>NSPrivacyAccessedAPICategoryFileTimestamp</string>
<key>NSPrivacyAccessedAPITypeReasons</key>
<array>
<string>C617.1</string>
</array>
</dict>
</array>
</dict>
</plist>
downloadFileからFile Transferプラグインへの移行
バージョン7.1.0以降、FilesystemプラグインのdownloadFile機能は非推奨となり、新しい@capacitor/file-transferプラグインの使用が推奨されて います。
File Transferプラグインのインストール
npm install @capacitor/file-transfer
npx cap sync
移行の例
変更前(Filesystemプラグインを使用):
import { Filesystem, Directory } from '@capacitor/filesystem';
await Filesystem.downloadFile({
url: 'https://example.com/file.pdf',
path: 'downloaded-file.pdf',
directory: Directory.Documents,
progress: true
});
// Progress events
Filesystem.addListener('progress', (progress) => {
console.log(`Downloaded ${progress.bytes} of ${progress.contentLength}`);
});
変更後(File Transferプラグインを使用):
import { FileTransfer } from '@capacitor/file-transfer';
import { Filesystem, Directory } from '@capacitor/filesystem';
// First get the full file path using Filesystem
const fileInfo = await Filesystem.getUri({
directory: Directory.Documents,
path: 'downloaded-file.pdf'
});
// Then use the FileTransfer plugin to download
await FileTransfer.downloadFile({
url: 'https://example.com/file.pdf',
path: fileInfo.uri,
progress: true
});
// Progress events
FileTransfer.addListener('progress', (progress) => {
console.log(`Downloaded ${progress.bytes} of ${progress.contentLength}`);
});
File Transferプラグインは、信頼性の向上、特定のエラーコードによるエラーハンドリングの改善に加え、アップロード機能も追加されています。
iOS
ファイルアプリにファイルを表示させるには、Info.plistで以下のキーをYESに設定する必要があります:
UIFileSharingEnabled(Application supports iTunes file sharing)LSSupportsOpeningDocumentsInPlace(Supports opening documents in place)
設定についてはiOSの設定を参照してください。