Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions ios/Classes/IcloudStorageSyncPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public class IcloudStorageSyncPlugin: NSObject, FlutterPlugin {
} else {
result(FlutterError(code: "INVALID_ARGUMENT", message: "containerId not provided", details: nil))
}
case "getICloudContainerUrl":
if let args = call.arguments as? [String: Any],
let containerId = args["containerId"] as? String {
getICloudContainerUrl(containerId: containerId, result: result)
} else {
result(FlutterError(code: "INVALID_ARGUMENT", message: "containerId not provided", details: nil))
}
case "isICloudAvailable":
isICloudAvailable(result)
case "upload":
upload(call, result)
case "delete":
Expand Down Expand Up @@ -157,6 +166,20 @@ public class IcloudStorageSyncPlugin: NSObject, FlutterPlugin {
}
}

private func getICloudContainerUrl(containerId: String, result: @escaping FlutterResult) {
guard let containerURL = FileManager.default.url(forUbiquityContainerIdentifier: containerId) else {
result(containerError)
return
}
DebugHelper.log("containerURL: \(containerURL.path)")
result(containerURL.path)
}

private func isICloudAvailable(_ result: @escaping FlutterResult) {
let isAvailable = FileManager.default.ubiquityIdentityToken != nil
result(isAvailable)
}


private func upload(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
guard let args = call.arguments as? Dictionary<String, Any>,
Expand Down
16 changes: 16 additions & 0 deletions lib/icloud_storage_sync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ class IcloudStorageSync {
return IcloudStorageSyncPlatform.instance.getPlatformVersion();
}

/// Returns the absolute path of the iCloud container directory for [containerId].
Future<String?> getContainerUrl({required String containerId}) {
return IcloudStorageSyncPlatform.instance.getContainerUrl(
containerId: containerId,
);
}

/// Indicates whether iCloud is available on this device for iCloud Drive Documents.
///
/// This checks if the system exposes a ubiquity identity token, which is
/// present only when the user is signed into iCloud and the feature is
/// enabled.
Future<bool> isICloudAvailable() {
return IcloudStorageSyncPlatform.instance.isICloudAvailable();
}

/// Gathers metadata for all files in the specified iCloud container.
///
/// [containerId] The ID of the iCloud container to query.
Expand Down
15 changes: 15 additions & 0 deletions lib/icloud_storage_sync_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ class MethodChannelIcloudStorageSync extends IcloudStorageSyncPlatform {
return version;
}

@override
Future<String?> getContainerUrl({required String containerId}) async {
return await methodChannel.invokeMethod<String>('getICloudContainerUrl', {
'containerId': containerId,
});
}

@override
Future<bool> isICloudAvailable() async {
final available = await methodChannel.invokeMethod<bool>(
'isICloudAvailable',
);
return available ?? false;
}

/// Gathers iCloud files and their metadata.
///
/// [containerId] is the iCloud container identifier.
Expand Down
12 changes: 12 additions & 0 deletions lib/icloud_storage_sync_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ abstract class IcloudStorageSyncPlatform extends PlatformInterface {
throw UnimplementedError('platformVersion() has not been implemented.');
}

/// Returns the absolute path of the iCloud container URL.
Future<String?> getContainerUrl({required String containerId}) async {
throw UnimplementedError('getContainerUrl() has not been implemented.');
}

/// Determines whether iCloud is available for iCloud Drive Documents by checking the ubiquity identity token.
///
/// Returns `true` when a token is present otherwise returns `false`.
Future<bool> isICloudAvailable() async {
throw UnimplementedError('isICloudAvailable() has not been implemented.');
}

/// Gathers all the files' metadata from the iCloud container.
///
/// [containerId] is the iCloud Container Id.
Expand Down
23 changes: 22 additions & 1 deletion macos/Classes/IcloudStorageSyncPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public class IcloudStorageSyncPlugin: NSObject, FlutterPlugin {
} else {
result(FlutterError(code: "INVALID_ARGUMENT", message: "containerId not provided", details: nil))
}
case "getICloudContainerUrl":
if let args = call.arguments as? [String: Any],
let containerId = args["containerId"] as? String {
getICloudContainerUrl(containerId: containerId, result: result)
} else {
result(FlutterError(code: "INVALID_ARGUMENT", message: "containerId not provided", details: nil))
}
case "isICloudAvailable":
isICloudAvailable(result)
case "upload":
upload(call, result)
case "download":
Expand Down Expand Up @@ -205,8 +214,20 @@ public class IcloudStorageSyncPlugin: NSObject, FlutterPlugin {

result(nil)
}


private func getICloudContainerUrl(containerId: String, result: @escaping FlutterResult) {
guard let containerURL = FileManager.default.url(forUbiquityContainerIdentifier: containerId) else {
result(containerError)
return
}
DebugHelper.log("containerURL: \(containerURL.path)")
result(containerURL.path)
}

private func isICloudAvailable(_ result: @escaping FlutterResult) {
let isAvailable = FileManager.default.ubiquityIdentityToken != nil
result(isAvailable)
}

private func addUploadObservers(query: NSMetadataQuery, eventChannelName: String) {
NotificationCenter.default.addObserver(forName: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: query, queue: query.operationQueue) { [self] (notification) in
Expand Down
9 changes: 9 additions & 0 deletions test/icloud_storage_sync_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class MockIcloudStorageSyncPlatform
@override
Future<String?> getPlatformVersion() => Future.value('42');

@override
Future<String?> getContainerUrl({required String containerId}) =>
throw UnimplementedError();

@override
Future<void> delete(
{required String containerId, required String relativePath}) {
Expand Down Expand Up @@ -54,6 +58,11 @@ class MockIcloudStorageSyncPlatform
StreamHandler<double>? onProgress}) {
throw UnimplementedError();
}

@override
Future<bool> isICloudAvailable() {
throw UnimplementedError();
}
}

void main() {
Expand Down