icloud - iOS 14:NSFilePresenter 没有收到回调/通知;适用于iOS13
问题描述
我有一个采用 NSFilePresenter 协议的 UIViewController。我已经实现了presentedItemDidChange() 以便接收存储在iCloud 上无处不在的容器中的文件的更改。我的实现在 iOS13.7 上运行良好,但在 iOS 14 上失败。
当我从 macOS 上的“Finder”之类的外部进程修改呈现的文件时,不会调用 presentItemDidChange()。
如果这是 iOS 14 中的一个已知错误,还是我在实现中遗漏了什么?
我的实现大致是这样的:
var m_basename = "textfile.txt"
var iCloudURL: URL? {
get {
guard let iCloudDocumentsBaseURL = FileManager.default.url(forUbiquityContainerIdentifier: iCloudContainerId) else {
return nil
}
let iCloudDocumentsURL = iCloudDocumentsBaseURL.appendingPathComponent("Documents").appendingPathComponent(m_basename)
return iCloudDocumentsURL
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
NSFileCoordinator.addFilePresenter(self)
/* do some iCloud availability check here ; not shown */
save(text: "Test string") { (success) in
if success {
print(#function, "Wrote file successfully")
} else {
print(#function, "Write file failed")
}
}
}
public func save(text: String, completion: @escaping (Bool) -> Void) {
if let presenterurl = self.presentedItemURL {
var errorMain: NSError?
// set presenter if we load a file that is currently being presented
var filePresenter: NSFilePresenter? = nil
if presenterurl == iCloudURL {
filePresenter = fPresenter
}
let coord = NSFileCoordinator(filePresenter: filePresenter)
coord.coordinate(writingItemAt: activeURL, options: .forReplacing, error: &errorMain) { (writeUrl) in
do {
try text.write(toFile: writeUrl.path, atomically: true, encoding: String.Encoding.utf8)
print(#function, "\(writeUrl) write OK")
completion(true)
} catch {
print(#function, "\(writeUrl) write failed: \(error.localizedDescription)")
completion(false)
}
}
if errorMain != nil {
print("Error: ", errorMain!.localizedDescription)
completion(false)
}
}
print(#function, "end of save() function")
}
// MARK: - NSFilePresenter
extension ViewController: NSFilePresenter {
public var presentedItemURL: URL? {
return iCloudURL
}
public var presentedItemOperationQueue: OperationQueue {
return OperationQueue.main
}
public func presentedItemDidChange() {
print(#function, "Loading file \(m_basename)")
}
}
谢谢
解决方案
推荐阅读
- r - 创建缺少数据的数据框
- python - 模拟 socket.recv()
- python - “E: 无法定位包 python3-pip”
- php - 无法抓取遍历多个页面的内容
- swift - Swift FileManager.ContentsOfDirectory 返回 nil
- arrays - PDF Merge VB.NET 使用搜索词作为数组。代码有效但复制了第一个 PDF?
- javascript - JQuery / Javascript在基于单选的表格中设置输入框
- multi-select - 如何使用 Blue Prism 在网络上的下拉菜单中多选选项
- angular - 用 forEach 对我的集合中的元素求和
- c# - 重启时如何保存和检索程序设置(.NET & C#)