Not able to perform read and write operation in iCloud in .Net Maui Mac

Kiran Mohanty 0 Reputation points
2024-05-13T10:26:23.9+00:00

I have set up the app identifier in Apple developer with the document URL set up for iCloud and updated the info.plist and entitlements according to this.

info.plist

<key>NSUbiquitousContainers</key>
<dict>
    <key>iCloud.com.abc.MyApp</key>
    <dict>
        <key>NSUbiquitousContainerIsDocumentScopePublic</key>
        <true/>
        <key>NSUbiquitousContainerName</key>
        <string>MyApp</string>
        <key>NSUbiquitousContainerSupportedFolderLevels</key>
        <string>Any</string>
    </dict>
</dict>

<key>UIFileSharingEnabled</key>
<true/>

<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>

<key>UIBackgroundModes</key>
<array>
    <string>fetch</string>
    <string>remote-notification</string>
</array>

<key>NSUbiquitousContainersUsageDescription</key>
<string>This app uses iCloud containers to store and sync documents.</string>

Entitlement.plist

<key>
<key>com.apple.developer.icloud-container-identifiers</key>
    <array>
        <string>iCloud.com.abc.MyApp</string>
    </array>
    <key>com.apple.developer.icloud-services</key>
    <array>
        <string>CloudDocuments</string>
    </array>
    <key>com.apple.developer.ubiquity-container-identifiers</key>
    <array>
        <string>iCloud.com.abc.MyApp</string>
    </array>
<key>

// Then I am using iCloud CRUD operation in app

// Code snippet


private void DeleteFolderFromICloud()
    {
        try {
            var iCloudDocumentsURL = NSFileManager.DefaultManager.GetUrlForUbiquityContainer(null);
            if (iCloudDocumentsURL != null)
            {
                var path = iCloudDocumentsURL.ToString().Replace("%C3%97", "x");
                var filepath = path.Replace("file://", string.Empty).Replace("%20", " ");
                var destinationdirectoryPath = Path.Combine(
filepath,"MyAppDocuments");
                if (Directory.Exists(destinationdirectoryPath))
                {
                    Directory.Delete(destinationdirectoryPath, recursive: true);
                }
            }
        }catch(Exception ex) {
            LogHandler.LogError(ex);
        }
    }

But in Delete operation gives Exception -> System.IO.IOException: Access to the path '/Users/USERABC/Library/Mobile Documents/iCloud~com~abc~MyApp/MyAppDocuments' is denied.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,461 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,984 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 27,126 Reputation points Microsoft Vendor
    2024-05-17T08:13:42.11+00:00

    Hello,

    Please use the CreateDirectory and Remove method of NSFileManager.

    Apple's API: createFileAtPath:contents:attributes: | Apple Developer Documentation

    try {
    			var iCloudDocumentsURL = NSFileManager.DefaultManager.GetUrlForUbiquityContainer(null);
    			if (iCloudDocumentsURL != null) { var path = iCloudDocumentsURL.ToString().Replace("%C3%97", "x");
    				var filepath = path.Replace("file://", string.Empty).Replace("%20", " ");
    				var destinationdirectoryPath = Path.Combine(filepath, "MyAppDocuments");
     
                    NSFileManager.DefaultManager.CreateDirectory(destinationdirectoryPath, true, null);
     
                    if (Directory.Exists(destinationdirectoryPath)) {
                        NSError err = new NSError();
                       NSFileManager.DefaultManager.Remove(destinationdirectoryPath,out err);
    				}
    				else
    				{
    				}
     
    			}
    		} catch (Exception ex) {
     
    			Console.WriteLine("--------");
    		}
    

    If you want to save the document, please refer to Using iCloud with Xamarin.iOS - Xamarin | Microsoft Learn (Xamarin support has been ended, this Doc applies to MAUI)

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments