Search code examples
c#iosmauiicloud

How to upload a file to iCloud in iOS .NET MAUI?


I have a project on iOS that you can visualize, download pdf from a service and I would like them to be uploaded and saved in a folder, that can be created, but I don't have much idea how to synchronize in iCloud.

Do I need to modify something in the developer account or any specific permission?

        string fileName = nombreFile + ".pdf";

        string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

        string path = System.IO.Path.Combine(documentsPath, fileName);

        using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
        {
            await documentStream.CopyToAsync(fileStream);
        }

I have this but I know this is saved on the device.


Solution

  • The first thing you will need to upload a file to ICloud is the entitlement: https://learn.microsoft.com/en-us/dotnet/maui/ios/entitlements?view=net-maui-8.0#icloud

    This contains the following xml for the entitlement:

    <key>com.apple.developer.icloud-container-identifiers</key>
    <array>
      <string>iCloud.com.companyname.test</string>
    </array>
    <key>com.apple.developer.ubiquity-kvstore-identifier</key>
    <string>$(AppIdentifierPrefix)$(CFBundleIdentifier)</string>
    

    While the following docs are for xamarin, they may be helpful in getting the correct response, as the following Microsoft vendor states: https://learn.microsoft.com/en-us/answers/questions/1665676/not-able-to-perform-read-and-write-operation-in-ic https://learn.microsoft.com/en-us/previous-versions/xamarin/ios/data-cloud/introduction-to-icloud#finding-and-opening-icloud-documents