微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iOS 14:NSFilePresenter无法获得回调/通知;适用于iOS13

如何解决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)")
  }
}

谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?