icloud_storage 0.2.0 copy "icloud_storage: ^0.2.0" to clipboard
icloud_storage: ^0.2.0 copied to clipboard

outdated

A flutter plugin for uploading and downloading files to and from iCloud.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import 'package:icloud_storage/icloud_storage.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  static const iCloudContainerId = '{your icloud container id}';

  void handleError(dynamic err) {
    if (err is PlatformException) {
      if (err.code == PlatformExceptionCode.iCloudConnectionOrPermission) {
        print(
            'Platform Exception: iCloud container ID is not valid, or user is not signed in for iCloud, or user denied iCloud permission for this app');
      } else {
        print('Platform Exception: ${err.message}; Details: ${err.details}');
      }
    } else {
      print(err.toString());
    }
  }

  Future<void> testListFile() async {
    try {
      final iCloudStorage = await ICloudStorage.getInstance(iCloudContainerId);
      final files = await iCloudStorage.listFiles();
      files.forEach((file) => print('--- List Files --- file: $file'));
    } catch (err) {
      handleError(err);
    }
  }

  Future<void> testWatchFile() async {
    try {
      final iCloudStorage = await ICloudStorage.getInstance(iCloudContainerId);
      final fileListStream = await iCloudStorage.watchFiles();
      final fileListSubscription = fileListStream.listen((files) {
        files.forEach((file) => print('--- Watch Files --- file: $file'));
      });

      Future.delayed(Duration(seconds: 10), () {
        fileListSubscription.cancel();
        print('--- Watch Files --- canceled');
      });
    } catch (err) {
      handleError(err);
    }
  }

  Future<void> testUploadFile() async {
    try {
      final iCloudStorage = await ICloudStorage.getInstance(iCloudContainerId);
      StreamSubscription<double> uploadProgressSubcription;
      var isUploadComplete = false;

      await iCloudStorage.startUpload(
        filePath: '{your local file}',
        destinationFileName: 'test_icloud_file',
        onProgress: (stream) {
          uploadProgressSubcription = stream.listen(
            (progress) => print('--- Upload File --- progress: $progress'),
            onDone: () {
              isUploadComplete = true;
              print('--- Upload File --- done');
            },
            onError: (err) => print('--- Upload File --- error: $err'),
            cancelOnError: true,
          );
        },
      );

      Future.delayed(Duration(seconds: 10), () {
        if (!isUploadComplete) {
          uploadProgressSubcription?.cancel();
          print('--- Upload File --- timed out');
        }
      });
    } catch (err) {
      handleError(err);
    }
  }

  Future<void> testDownloadFile() async {
    try {
      final iCloudStorage = await ICloudStorage.getInstance(iCloudContainerId);
      StreamSubscription<double> downloadProgressSubcription;
      var isDownloadComplete = false;

      await iCloudStorage.startDownload(
        fileName: 'test_icloud_file',
        destinationFilePath: '{your destination file path}',
        onProgress: (stream) {
          downloadProgressSubcription = stream.listen(
            (progress) => print('--- Download File --- progress: $progress'),
            onDone: () {
              isDownloadComplete = true;
              print('--- Download File --- done');
            },
            onError: (err) => print('--- Download File --- error: $err'),
            cancelOnError: true,
          );
        },
      );

      Future.delayed(Duration(seconds: 20), () {
        if (!isDownloadComplete) {
          downloadProgressSubcription?.cancel();
          print('--- Download File --- timed out');
        }
      });
    } catch (err) {
      handleError(err);
    }
  }

  Future<void> testDeleteFile() async {
    try {
      final iCloudStorage = await ICloudStorage.getInstance(iCloudContainerId);
      await iCloudStorage.delete('test_icloud_file');
    } catch (err) {
      handleError(err);
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('icloud_storage plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              FlatButton(
                child: Text('List File'),
                onPressed: testListFile,
              ),
              FlatButton(
                child: Text('Watch File'),
                onPressed: testWatchFile,
              ),
              FlatButton(
                child: Text('Start Upload'),
                onPressed: testUploadFile,
              ),
              FlatButton(
                child: Text('Start Download'),
                onPressed: testDownloadFile,
              ),
              FlatButton(
                child: Text('Delete File'),
                onPressed: testDeleteFile,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
65
likes
0
pub points
91%
popularity

Publisher

unverified uploader

A flutter plugin for uploading and downloading files to and from iCloud.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on icloud_storage