メインコンテンツへスキップ

はじめに

GitHub

AIアシストセットアップを使ってプラグインをインストールできます。以下のコマンドで、お使いのAIツールに Capgo スキルを追加してください:

ターミナルウィンドウ
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

次に、以下のプロンプトを使用します:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-downloader` plugin in my project.

手動セットアップを希望する場合は、以下のコマンドでプラグインをインストールし、下記のプラットフォーム別の手順に従ってください:

ターミナルウィンドウ
bun add @capgo/capacitor-downloader
bunx cap sync
import { CapacitorDownloader } from '@capgo/capacitor-downloader';

クイックスタート

「Quick start」セクション

指定したフォルダへのダウンロード、進捗の監視、およびサポートされている環境でのアプリ再起動後のレジュームに対応しています。

import { CapacitorDownloader } from '@capgo/capacitor-downloader';
const progressListener = await CapacitorDownloader.addListener('downloadProgress', (event) => {
if (event.id === 'offline-pack') {
console.log(event.progress);
}
});
const task = await CapacitorDownloader.download({
id: 'offline-pack',
url: 'https://cdn.example.com/large-pack.zip',
destination: 'downloads/offline-pack.zip',
});
await CapacitorDownloader.pause({ id: 'offline-pack' });
await CapacitorDownloader.resume({ id: 'offline-pack' });
import { Capacitor } from '@capacitor/core';
import { Directory, Filesystem } from '@capacitor/filesystem';
const relativePath = 'downloads/offline-pack.zip';
const path =
Capacitor.getPlatform() === 'ios'
? (await Filesystem.getUri({ directory: Directory.Documents, path: relativePath })).uri
: relativePath;
const info = await CapacitorDownloader.getFileInfo({ path });

よくある落とし穴

「Common pitfalls」セクション
  • 保存先: 以下から選択 destination 各プラットフォームでアプリが読み取れる場所を指定してください。
  • バックグラウンド: iOSとAndroidではバックグラウンドでダウンロードを実行します。Webでは、 download() は未実装です。代わりにブラウザのダウンロードフローを使用してください。
  • ステータス: 使用 checkStatus アプリのプロセスが実行されている間のみ可能です。iOSでは、アプリの再起動後にタスクIDは復元されません。

新しいダウンロードタスクを開始します。

import { CapacitorDownloader } from '@capgo/capacitor-downloader';
const task = await CapacitorDownloader.download({
id: 'my-download',
url: 'https://example.com/file.pdf',
destination: 'downloads/file.pdf'
});

実行中のダウンロードを一時停止します。 ダウンロードは後で同じ位置から再開できます。

import { CapacitorDownloader } from '@capgo/capacitor-downloader';
await CapacitorDownloader.pause({ id: 'id-123' });

一時停止中のダウンロードを再開します。 中断した箇所から続行します。

import { CapacitorDownloader } from '@capgo/capacitor-downloader';
await CapacitorDownloader.resume({ id: 'id-123' });

ダウンロードを停止し、完全にキャンセルします。 ダウンロード済みのデータは削除されます。

import { CapacitorDownloader } from '@capgo/capacitor-downloader';
await CapacitorDownloader.stop({ id: 'id-123' });

ダウンロードの現在のステータスを確認します。

import { CapacitorDownloader } from '@capgo/capacitor-downloader';
const result = await CapacitorDownloader.checkStatus({ id: 'id-123' });
console.log(result);

ダウンロード済みファイルの情報を取得します。

import { CapacitorDownloader } from '@capgo/capacitor-downloader';
const result = await CapacitorDownloader.getFileInfo({ path: 'path/to/file' });
console.log(result);

ダウンロードを開始するための設定オプション。

export interface DownloadOptions {
/** Unique identifier for this download task */
id: string;
/** URL of the file to download */
url: string;
/** Local file path where the download will be saved */
destination: string;
/** Optional HTTP headers to include in the request */
headers?: { [key: string]: string };
/** Network type requirement for download */
network?: 'cellular' | 'wifi-only';
/** Download priority level */
priority?: 'high' | 'normal' | 'low';
}

ダウンロードタスクの現在の状態と進捗を表します。

export interface DownloadTask {
/** Unique identifier for the download task */
id: string;
/** Download progress from 0 to 100 */
progress: number;
/** Current state of the download */
state: 'PENDING' | 'RUNNING' | 'PAUSED' | 'DONE' | 'ERROR';
}

このページはプラグインの src/definitions.tsから生成されています。上流で公開 API が変更されたら、sync を再実行してください。

以下を使用している場合: はじめに ダッシュボードと API の操作を計画するには、以下と連携してください @capgo/capacitor-downloader の使用方法 @capgo/capacitor-downloader でネイティブ機能を使用する場合、 API の概要 API Overview の実装の詳細については、 はじめに Introduction の実装の詳細については、 API キー API Keys の実装の詳細については、 デバイス Devices の実装の詳細については。