title: Best way to use iCloud Documents Storage
body: <p>I am currently using local storage in my iOS App. The user data is stored in the Document Directory and now I am planning to use iCloud Documents storage instead.</p>
  
  <p>Here is how I intend to do it :</p>
  
  <ol>
  <li><p>Checking if iCloud is available on the device</p></li>
  <li><p>If yes, use URLForUbiquityContainerIdentifier to get the iCloud container URL</p></li>
  <li><p>Save new files and documents to this new URL</p></li>
  </ol>
  
  <p>For that I am using this code that will return the URL of the document folder (iCloud or local)
       </p>
  
  <pre><code>class CloudDataManager {
  
      class func getDocumentDiretoryURL() -&gt; NSURL {
          let localDocumentsURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: .UserDomainMask).last! as NSURL
          let iCloudDocumentsURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil)?.URLByAppendingPathComponent("Documents")
  
          if userDefault.boolForKey("useCloud") &amp;&amp; iCloudDocumentsURL != nil  {
              return iCloudDocumentsURL!
          } else {
              return localDocumentsURL
          }
      }
  }
  </code></pre>
  
  <p>Is it the best practice? 
  I am worried problems will occur if one day iCloud isn't available so the local directory will be used instead of the cloud container and will be empty.
  Thank you.</p>