Forum Xamarin.iOS

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Unable to copy file to iCloud Drive

MatteoRisoMatteoRiso USMember ✭✭
edited December 2017 in Xamarin.iOS

I wrote a test app ("MyApp") to see how to I can write a file locally in my iPhone and, then, to copy it to iCloud Drive.
I read almost every tutorial, I've checked entitlements and provisioning files, and I've also edited my Info.plist as suggested in https://developer.apple.com/library/content/documentation/General/Conceptual/iCloudDesignGuide/Chapters/DesigningForDocumentsIniCloud.html#//apple_ref/doc/uid/TP40012094-CH2-SW20

This is the code I wrote to perform my test:

public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            // Perform any additional setup after loading the view, typically from a nib.

            long dd = DateTime.Now.ToFileTime();
            NSError error;
            NSUrl url = NSFileManager.DefaultManager.GetUrlForUbiquityContainer("iCloud.com.zipgenius.MyApp");
            Debug.WriteLine("UbiquityURL "+url.Path);
            var token = NSFileManager.DefaultManager.UbiquityIdentityToken;
            Debug.WriteLine("Token " + token.ToString());
            var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            Debug.WriteLine("Documents "+documents);
            var filename = Path.Combine(documents, dd.ToString()+".txt");
            File.WriteAllText(filename, "Write this text into a file");

            Debug.WriteLine("Filename "+filename);
            NSUrl sUrl = new NSUrl("file://" + filename);
            Debug.WriteLine("sUrl "+sUrl.AbsoluteUrl);
            NSUrl dUrl = url.Append(dd.ToString() + ".txt", false);
            Debug.WriteLine("dUrl "+dUrl.AbsoluteUrl);
            //NSFileManager.DefaultManager.Copy(sUrl, dUrl, out error);
            //if (error != null)
                //Debug.WriteLine("Error " + error.ToString());
            NSFileManager.DefaultManager.SetUbiquitous(true,sUrl,dUrl, out error);
            if (error != null)
                Debug.WriteLine("Error2 " + error.ToString());
        }

This is the debug log I get when I run my app:

UbiquityURL /private/var/mobile/Library/Mobile Documents/iCloud~com~zipgenius~MyApp Token <3599b99a b7a019bf 71b5b03a 504fe11e b6a79c4a> Documents /var/mobile/Containers/Data/Application/72C65A45-CBF7-4CD6-B770-39A1B14F1BA6/Documents Filename /var/mobile/Containers/Data/Application/72C65A45-CBF7-4CD6-B770-39A1B14F1BA6/Documents/131588619466944440.txt sUrl file:///var/mobile/Containers/Data/Application/72C65A45-CBF7-4CD6-B770-39A1B14F1BA6/Documents/131588619466944440.txt dUrl file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~zipgenius~MyApp/131588619466944440.txt

I get no error but I can't see my file in iCloud Drive (nor I can see a "MyApp" folder in it, as per Apple documentation).
What am I doing wrong?

Note: I both tested NSFileManager.DefaultManager.Copy and .SetUbiquitous but with no change in results.

Sign In or Register to comment.