首页 > 解决方案 > UIAlertController 失败后打开 UIActivityViewController

问题描述

我需要一个功能,通过电子邮件共享一些 .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)

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

谢谢!

标签: iosswiftuialertcontrolleruiactivityviewcontroller

解决方案


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

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

// 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)
}

推荐阅读