title: iOS Swift 3 Copy File to iCloud Drive Programatically
body: <p>In my I'm having documents download option. When users downloading documents from my app I need to store it to users iCloud Drive which was install in users mobile already. I have configured iCloud in both web and in Xcode, but problem is I'm not able to copy files to iCloud Drive correctly. File was downloaded successfully, and also it moving to iCloud but files won't appearing in iCloud Drive App. Here is my tried code:</p>
  
  <pre><code>&lt;key&gt;NSUbiquitousContainers&lt;/key&gt;
      &lt;dict&gt;
          &lt;key&gt;iCloud.MyAppBundleIdentifier&lt;/key&gt;
          &lt;dict&gt;
              &lt;key&gt;NSUbiquitousContainerIsDocumentScopePublic&lt;/key&gt;
              &lt;true/&gt;
              &lt;key&gt;NSUbiquitousContainerName&lt;/key&gt;
              &lt;string&gt;iCloudDriveDemo&lt;/string&gt;
              &lt;key&gt;NSUbiquitousContainerSupportedFolderLevels&lt;/key&gt;
              &lt;string&gt;Any&lt;/string&gt;
          &lt;/dict&gt;
      &lt;/dict&gt;
  </code></pre>
  
  <p>And Here is My iCloud Storage Code:</p>
  
  <pre><code>func DownloadDocumnt()
      {
          print("Selected URL: \(self.SelectedDownloadURL)")
          let fileURL = URL(string: "\(self.SelectedDownloadURL)")!
  
          let documentsUrl:URL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
          let destinationFileUrl = documentsUrl.appendingPathComponent("Libra-\(self.SelectedDownloadFileName)")
  
          let sessionConfig = URLSessionConfiguration.default
          let session = URLSession(configuration: sessionConfig)
  
          let request = URLRequest(url:fileURL)
  
          let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
              if let tempLocalUrl = tempLocalUrl, error == nil
              {
                  if let statusCode = (response as? HTTPURLResponse)?.statusCode
                  {
                      print("Successfully downloaded. Status code: \(statusCode)")
                  }
                  do
                  {
                      if(FileManager.default.fileExists(atPath: destinationFileUrl.path))
                      {
                          try FileManager.default.removeItem(at: destinationFileUrl)
                          try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
                      }
                      else
                      {
                          try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
                      }
  
                      if let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")
                      {
  
                          if(!FileManager.default.fileExists(atPath: iCloudDocumentsURL.path, isDirectory: nil))
                          {
                              try FileManager.default.createDirectory(at: iCloudDocumentsURL, withIntermediateDirectories: true, attributes: nil)
                          }
                      }
  
                      let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents").appendingPathComponent("Libra-\(self.SelectedDownloadFileName)")
  
                      if let iCloudDocumentsURL = iCloudDocumentsURL
                      {
                          var isDir:ObjCBool = false
                          if(FileManager.default.fileExists(atPath: iCloudDocumentsURL.path, isDirectory: &amp;isDir))
                          {
                              try FileManager.default.removeItem(at: iCloudDocumentsURL)
                              try FileManager.default.copyItem(at: tempLocalUrl, to: iCloudDocumentsURL)
                          }
                          else
                          {
                              try FileManager.default.copyItem(at: destinationFileUrl, to: iCloudDocumentsURL)
                          }
                      }
  
                  }
                  catch (let writeError)
                  {
                      print("Error creating a file \(destinationFileUrl) : \(writeError)")
                  }
              }
              else
              {
                  print("Error took place while downloading a file. Error description");
              }
          }
          task.resume()
      }
  </code></pre>