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:
<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.MyAppBundleIdentifier</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>iCloudDriveDemo</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>
And Here is My iCloud Storage 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: &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()
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…