Xamarin ios Cloud file copy location issue

540 Views Asked by At

I had connected iCloud using xamarin forms ios. My file copied to icloud without any error. But it saved in file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~companyname~MobileDoc/ path.

When i checked inside iCloud my file not showing it. But when i search that file, its location shows as iCloud --> MobileDoc--> File.txt. But when i checked inside the iCloud there are no Folder call MobileDoc.

This is the example i tried Here

This is the code i am using to save the document.

var uburl = NSFileManager.DefaultManager.GetUrlForUbiquityContainer(null);

                 if (uburl == null)
                 {
                     HasiCloud = false;
                 }
                else
                 {
                     HasiCloud = true;
                     iCloudUrl = uburl;

                     var docsFolder = Path.Combine(iCloudUrl.Path, "Documents"); 
             var filePath = Path.Combine(docsFolder, fileName);

         FileService.Save(filePath, attachment.Content);
                     if (option == false)
                         FileService.Instance.Open(filePath);
                }
1

There are 1 best solutions below

5
On

Saving iCloud Documents

To add a UIDocument to iCloud you can call UIDocument.Save directly (for new documents only) or move an existing file using NSFileManager.DefaultManager.SetUbiquitious. The example code creates a new document directly in the ubiquity container with this code (there are two completion handlers here, one for the Save operation and another for the Open):

var docsFolder = Path.Combine (iCloudUrl.Path, "Documents"); // NOTE: Documents folder is user-accessible in Settings
var docPath = Path.Combine (docsFolder, MonkeyDocFilename);
var ubiq = new NSUrl (docPath, false);
var monkeyDoc = new MonkeyDocument (ubiq);
monkeyDoc.Save (monkeyDoc.FileUrl, UIDocumentSaveOperation.ForCreating, saveSuccess => {
Console.WriteLine ("Save completion:" + saveSuccess);
if (saveSuccess) {
    monkeyDoc.Open (openSuccess => {
        Console.WriteLine ("Open completion:" + openSuccess);
        if (openSuccess) {
            Console.WriteLine ("new document for iCloud");
            Console.WriteLine (" == " + monkeyDoc.DocumentString);
            viewController.DisplayDocument (monkeyDoc);
        } else {
            Console.WriteLine ("couldn't open");
        }
    });
} else {
    Console.WriteLine ("couldn't save");
}

According to document ,when you save file to icloud,you need to use UIDocument .This will be Safe and stable.