gpt4 book ai didi

iOS Swift 3 以编程方式将文件复制到 iCloud Drive

转载 作者:可可西里 更新时间:2023-11-01 06:21:24 26 4
gpt4 key购买 nike

在我有文档下载选项中。当用户从我的应用程序下载文档时,我需要将其存储到已经安装在用户移动设备中的用户 iCloud Drive。我已经在 Web 和 Xcode 中配置了 iCloud,但问题是我无法将文件正确复制到 iCloud Drive。文件已成功下载,并且它也移动到 iCloud,但文件不会出现在 iCloud Drive App 中。这是我尝试过的代码:

<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>

这是我的 iCloud 存储代码:

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()
}

最佳答案

如果文件存储在应用程序的 iCloud 容器中,则您无法直接在 iCloud Drive 应用程序中看到这些文件。

    let iCloudDocumentsURL = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")

当您使用 UbiquityContainerIdentifier 路径将文件保存到 iCloud 时,它会将文件保存在应用程序的 iCloud 容器中。您应用的容器受到保护,无法直接在 iCloud Drive 中查看。

但是我们可以通过设置->点击您的iCloud->iCloud->iCloud->管理存储->您的应用名称->

关于iOS Swift 3 以编程方式将文件复制到 iCloud Drive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43934427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com