简体   繁体   中英

How to Upload and download files to iCloud in xamarin ios

I want to upload documents to iCloud . and also need to download it from iCloud and edit. i am new to xamarin ios applications. couldn't find detailed example for this. please help.

The first step in using iCloud Document Storage is to determine whether iCloud is enabled, and if so the location of the "ubiquity container" (the directory where iCloud-enabled files are stored on the device).

This code is in the AppDelegate.FinishedLaunching method of the sample.

ThreadPool.QueueUserWorkItem (_ => {
CheckingForiCloud = true;
Console.WriteLine ("Checking for iCloud");
var uburl = NSFileManager.DefaultManager.GetUrlForUbiquityContainer (null);
// OR instead of null you can specify "TEAMID.com.your-company.ApplicationName"

if (uburl == null) {
    HasiCloud = false;
    Console.WriteLine ("Can't find iCloud container, check your provisioning profile and entitlements");

    InvokeOnMainThread (() => {
        var alertController = UIAlertController.Create ("No \uE049 available",
        "Check your Entitlements.plist, BundleId, TeamId and Provisioning Profile!", UIAlertControllerStyle.Alert);
        alertController.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Destructive, null));
        viewController.PresentViewController (alertController, false, null);
    });
 } else { // iCloud enabled, store the NSURL for later use
    HasiCloud = true;
    iCloudUrl = uburl;
    Console.WriteLine ("yyy Yes iCloud! {0}", uburl.AbsoluteUrl);
 }
 CheckingForiCloud = false;
});

Creating a UIDocument Subclass

public class MonkeyDocument : UIDocument
{
  // the 'model', just a chunk of text in this case; must easily convert to NSData
     NSString dataModel;
  // model is wrapped in a nice .NET-friendly property
   public string DocumentString 
   {
    get {
        return dataModel.ToString ();
        }

    set {
        dataModel = new NSString (value);
        }
    }

    public MonkeyDocument (NSUrl url) : base (url)
    {
      DocumentString = "(default text)";
    }
    // contents supplied by iCloud to display, update local model and display (via notification)
    public override bool LoadFromContents (NSObject contents, string typeName, out NSError outError)
    {
      outError = null;

      Console.WriteLine ("LoadFromContents({0})", typeName);

      if (contents != null)
        dataModel = NSString.FromData ((NSData)contents, NSStringEncoding.UTF8);

     // LoadFromContents called when an update occurs
      NSNotificationCenter.DefaultCenter.PostNotificationName.("monkeyDocumentModified", this);
     return true;
    }
   // return contents for iCloud to save (from the local model)
    public override NSObject ContentsForType (string typeName, out NSError outError)
    {
     outError = null;

     Console.WriteLine ("ContentsForType({0})", typeName);
     Console.WriteLine ("DocumentText:{0}",dataModel);

     NSData docData = dataModel.Encode (NSStringEncoding.UTF8);
     return docData;
    }
}

Saving iCloud Documents

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");
}

For more detail about iCloud you can refer here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM