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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...