UIAlertController 失败后打开 UIActivityViewController

open UIActivityViewController after UIAlertController fails

我需要一个功能,通过电子邮件共享一些 .csv 文件。

所以首先我要将 .csv 文件保存到文档目录,然后我尝试打开一个 UIActivityViewController。

但这失败了。

这是我的代码:

saveprojectfile() // Here are the UIAlertController calls, these calls needs some time

let activityVC = UIActivityViewController(activityItems: [path], applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = self.view

self.present(activityVC, animated: true, completeion: nil)

任何提示,为什么这不起作用?

谢谢!

显示UIAlertController时正在执行UIActivityViewController代码,但在显示警报控制器时无法呈现。我还以为这会崩溃。

您应该在警报控制器操作的完成处理程序中显示 activity 视图控制器。

// Code for alert controller (was in your saveprojectfile() function
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: yourFunction)) // OK action handler added
self.present(alert, animated: true, completion: nil) // Completion handler here will not work as it fires on completion of presenting, not dismissing

func yourFunction() {
    // Present the action controller here
    let activityVC = UIActivityViewController(activityItems: [path], applicationActivities: nil)
    activityVC.popoverPresentationController?.sourceView = self.view
    self.present(activityVC, animated: true, completeion: nil)
}