所以我正在努力从 Xcode 导出一个 .txt 文件,但每当我尝试导出它时,比如通过电子邮件,它会创建一个没有附件和空消息的电子邮件。当我尝试将其导出到 iCloud Drive 时,会出现一秒钟的白屏,然后消失。当我检查 iCloud Drive 时,文件不存在。这里有什么问题?
let message = testLabel.text
func getDocumentsDirectory() -> NSString {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory as NSString
}
let filePath = getDocumentsDirectory().appendingPathComponent("matrixFile.txt")
do{
try message?.write(toFile: filePath, atomically: true, encoding: String.Encoding.utf8)
}catch let error as NSError{
testLabel.text = String(describing: error)
}
let documentInteractionController = UIDocumentInteractionController()
documentInteractionController.url = NSURL.fileURL(withPath: filePath)
documentInteractionController.uti = "public.data, public.content"
documentInteractionController.name = "matrixFile"
documentInteractionController.presentOptionsMenu(from: view.frame, in: view, animated: true)
最佳答案
问题是您将文档交互 Controller 声明为调用它的方法中的常量,例如:
func someButton() {
let controller = UIDocumentInteractionController...
}
它不会那样工作。交互 Controller 需要是呈现它的类的实例变量,并且每次使用新的 url 呈现它时都应该重新初始化它,如下所示:
var controller: UIDocumentInteractionController!
func someButton() {
controller = UIDocumentInteractionController(url: someURL)
controller.presentOptionsMenu...
}
为什么?我不知道,但如果您认为这很荒谬,请提交错误报告。
关于swift - UIDocumentInteractionController 文件附件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43174532/