Skip to content

TagSpaces is an offline, open source, document manager with tagging support

chore: integrating support for icloud

Ilian Sapundshiev8b1a770d0b44
diff --git a/capacitor/ios/App/App/ICloudPlugin.swift b/capacitor/ios/App/App/ICloudPlugin.swift
index dff7344e7..ab70d97c2 100644
--- a/capacitor/ios/App/App/ICloudPlugin.swift
+++ b/capacitor/ios/App/App/ICloudPlugin.swift
@@ -10,19 +10,38 @@ public class ICloudPlugin: CAPPlugin, CAPBridgedPlugin {
CAPPluginMethod(name: "startDownload", returnType: CAPPluginReturnPromise)
]
+ private static let containerID = "iCloud.org.tagspaces.mobileapp"
+
@objc func getUbiquityContainer(_ call: CAPPluginCall) {
- // forUbiquityContainerIdentifier: nil → use the first container declared
- // in the entitlement (iCloud.org.tagspaces.mobileapp). Returns nil if
- // iCloud Drive is disabled, the user isn't signed in, or the container
- // hasn't been provisioned yet on Apple's side.
+ // Must run off the main thread — this call can block while iCloud sets
+ // up the container. Try the explicit container identifier first (passing
+ // nil is unreliable), then fall back to nil (first entitlement container).
DispatchQueue.global(qos: .userInitiated).async {
- guard let containerURL = FileManager.default.url(forUbiquityContainerIdentifier: nil) else {
- call.resolve(["available": false])
+ let fm = FileManager.default
+ let token = fm.ubiquityIdentityToken
+ CAPLog.print("[ICloud] ubiquityIdentityToken present: \(token != nil)")
+
+ let containerURL = fm.url(forUbiquityContainerIdentifier: ICloudPlugin.containerID)
+ ?? fm.url(forUbiquityContainerIdentifier: nil)
+
+ guard let containerURL = containerURL else {
+ // token == nil → user not signed into iCloud / iCloud Drive off.
+ // token != nil but url nil → entitlement/container provisioning issue.
+ let reason = token == nil
+ ? "not-signed-in"
+ : "container-unavailable"
+ CAPLog.print("[ICloud] container URL is nil, reason: \(reason)")
+ call.resolve(["available": false, "reason": reason])
return
}
+
let documentsURL = containerURL.appendingPathComponent("Documents", isDirectory: true)
- try? FileManager.default.createDirectory(at: documentsURL,
- withIntermediateDirectories: true)
+ do {
+ try fm.createDirectory(at: documentsURL, withIntermediateDirectories: true)
+ } catch {
+ CAPLog.print("[ICloud] createDirectory failed: \(error.localizedDescription)")
+ }
+ CAPLog.print("[ICloud] resolved documentsPath: \(documentsURL.path)")
call.resolve([
"available": true,
"containerPath": containerURL.path,
diff --git a/src/renderer/components/dialogs/components/LocalForm.tsx b/src/renderer/components/dialogs/components/LocalForm.tsx
index df899f695..3dc2d66d3 100644
--- a/src/renderer/components/dialogs/components/LocalForm.tsx
+++ b/src/renderer/components/dialogs/components/LocalForm.tsx
@@ -20,7 +20,8 @@ import AppConfig from '-/AppConfig';
import TsButton from '-/components/TsButton';
import TsTextField from '-/components/TsTextField';
import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
-import { selectDirectoryDialog } from '-/services/utils-io';
+import { useNotificationContext } from '-/hooks/useNotificationContext';
+import { getICloudContainer, selectDirectoryDialog } from '-/services/utils-io';
import FormControl from '@mui/material/FormControl';
import FormHelperText from '@mui/material/FormHelperText';
import Grid from '@mui/material/Grid';
@@ -41,6 +42,37 @@ function LocalForm(props: Props) {
const { errorTextPath, errorTextName, setName, setPath, path, name } = props;
const { t } = useTranslation();
const { findLocation } = useCurrentLocationContext();
+ const { showNotification } = useNotificationContext();
+
+ const useICloudDrive = () => {
+ getICloudContainer()
+ .then((result: any) => {
+ console.log('getICloudContainer result: ' + JSON.stringify(result));
+ if (result && result.available && result.documentsPath) {
+ setPath(result.documentsPath);
+ if (name.length < 1) {
+ setName('iCloud Drive');
+ }
+ } else {
+ const reason =
+ result && result.reason ? ' (' + result.reason + ')' : '';
+ showNotification(
+ t('core:iCloudNotAvailable') + reason,
+ 'warning',
+ true,
+ );
+ }
+ return true;
+ })
+ .catch((err) => {
+ console.log('getICloudContainer failed with: ' + err);
+ showNotification(
+ t('core:iCloudNotAvailable') + ' (' + err + ')',
+ 'warning',
+ true,
+ );
+ });
+ };
const openDirectory = () => {
selectDirectoryDialog()
@@ -101,6 +133,17 @@ function LocalForm(props: Props) {
ext. sd-card
</FormHelperText>
)}
+ {AppConfig.isCapacitoriOS && (
+ <TsButton
+ size="small"
+ data-tid="useICloudDriveTID"
+ onClick={useICloudDrive}
+ variant="outlined"
+ sx={{ marginTop: '8px', alignSelf: 'flex-start' }}
+ >
+ {t('core:useICloudDrive')}
+ </TsButton>
+ )}
</FormControl>
</Grid>
<Grid size={12}>
diff --git a/src/renderer/locales/en/core.json b/src/renderer/locales/en/core.json
index 89b4cbfcb..86f1b1a93 100644
--- a/src/renderer/locales/en/core.json
+++ b/src/renderer/locales/en/core.json
@@ -574,6 +574,8 @@
"picturesFolder": "Pictures",
"videosFolder": "Videos",
"iCloudFolder": "iCloud Drive",
+ "useICloudDrive": "Use iCloud Drive",
+ "iCloudNotAvailable": "iCloud Drive is not available. Make sure you are signed in to iCloud and iCloud Drive is enabled in Settings.",
"startImportButton": "Import",
"startExportButton": "Export",
"extractGeoCoordinates": "Extract geo-coordinates from files as tags",