gpt4 book ai didi

ios - SWIFT - 上传文件到 icloud 文件没有完成

转载 作者:行者123 更新时间:2023-12-05 07:14:28 27 4
gpt4 key购买 nike

我使用了下面页面上的教程,由于某种原因,文件开始上传并在控制台上显示为 95%,但从未显示“备份上传完成”消息。该应用仅用于测试sample.mp4文件的上传。

https://medium.com/swlh/backup-your-applications-data-with-icloud-and-track-progress-48a00ebd2891

//  BackupToIcloudController.swift


import Foundation

import UIKit

class BackupToIcloudController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let button = UIButton(frame: CGRect(x: UIScreen.main.bounds.midX - 150, y: UIScreen.main.bounds.midY - 20, width: 300, height: 40))
button.setTitle("Upload to iCloud", for: .normal)
button.addTarget(self, action: #selector(self.uploadToCloud), for: .touchUpInside)
button.backgroundColor = .blue
view.addSubview(button)
}

@objc func uploadToCloud() {
let backup = Backup.init()
try? backup.startBackup()
}

@IBAction func back(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
}
//  Backup.swift


import Foundation

class Backup: NSObject {

var query: NSMetadataQuery!

override init() {
super.init()
initialiseQuery()
addNotificationObservers()
}
func initialiseQuery() {

query = NSMetadataQuery.init()
query.operationQueue = .main
query.searchScopes = [NSMetadataQueryUbiquitousDataScope]
query.predicate = NSPredicate(format: "%K LIKE %@", NSMetadataItemFSNameKey, "sample.mp4")
}

func startBackup() throws {
guard let fileURL = Bundle.main.url(forResource: "sample", withExtension: "mp4") else {
return

}
guard let containerURL = FileManager.default.url(forUbiquityContainerIdentifier: "iCloud.mylojastore.com.backup") else { return }

if !FileManager.default.fileExists(atPath: containerURL.path) {
try FileManager.default.createDirectory(at: containerURL, withIntermediateDirectories: true, attributes: nil)
}
let backupFileURL = containerURL.appendingPathComponent("sample.mp4")
if FileManager.default.fileExists(atPath: backupFileURL.path) {
try FileManager.default.removeItem(at: backupFileURL)
try FileManager.default.copyItem(at: fileURL, to: backupFileURL)
} else {
try FileManager.default.copyItem(at: fileURL, to: backupFileURL)
}

query.operationQueue?.addOperation({ [weak self] in
_ = self?.query.start()
self?.query.enableUpdates()
})
}
func addNotificationObservers() {

NotificationCenter.default.addObserver(forName: NSNotification.Name.NSMetadataQueryDidStartGathering, object: query, queue: query.operationQueue) { (notification) in
self.processCloudFiles()
}

NotificationCenter.default.addObserver(forName: NSNotification.Name.NSMetadataQueryDidUpdate, object: query, queue: query.operationQueue) { (notification) in
self.processCloudFiles()
}
NotificationCenter.default.addObserver(forName: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: query, queue: query.operationQueue) { (notification) in
self.processCloudFiles()
}
NotificationCenter.default.addObserver(forName: NSNotification.Name.NSMetadataQueryGatheringProgress, object: query, queue: query.operationQueue) { (notification) in
print("progress")
}
}

@objc func processCloudFiles() {

if query.results.count == 0 { return }
var fileItem: NSMetadataItem?
var fileURL: URL?

for item in query.results {

guard let item = item as? NSMetadataItem else { continue }
guard let fileItemURL = item.value(forAttribute: NSMetadataItemURLKey) as? URL else { continue }
if fileItemURL.lastPathComponent.contains("sample.mp4") {
fileItem = item
fileURL = fileItemURL
}
}

let fileValues = try? fileURL!.resourceValues(forKeys: [URLResourceKey.ubiquitousItemIsUploadingKey])
if let fileUploaded = fileItem?.value(forAttribute: NSMetadataUbiquitousItemIsUploadedKey) as? Bool, fileUploaded == true, fileValues?.ubiquitousItemIsUploading == false {
print("backup upload complete")

} else if let error = fileValues?.ubiquitousItemUploadingError {
print("upload error---", error.localizedDescription)

} else {
if let fileProgress = fileItem?.value(forAttribute: NSMetadataUbiquitousItemPercentUploadedKey) as? Double {
print("uploaded percent ---", fileProgress)
}
}
}
}

最佳答案

看来我们必须在 plist 中指定容器才能工作。

将以下内容添加到您的 plist。

<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.iCloudBackup</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>iCloudBackup</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>

你应该看到这个。 enter image description here

关于ios - SWIFT - 上传文件到 icloud 文件没有完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59906079/

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