swift - 无论如何,我是否可以在 Swift 中使用 MFMessageComposeViewController() 通过文本发送 PDF 和 PNG?
问题描述
每当我尝试使用 .addAttachmentURL 时,它都不会附加任何内容。ViewController 在文本正文中没有任何内容。URL 是我的文件默认值中 pdf数据的路径(我不知道这是否会有所不同)。有什么方法可以通过这样的文本发送 PDF 吗?通过查看文档或 StackOverflow,我没有找到任何东西。此外,我还没有实现它,但我想知道是否有办法将 PNG 也附加到我与 PDF 一起发送的这条消息中。
func getFileManager() -> NSString {
let filePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString)
return filePath
}
func displayMessageInterface() {
let composeVC = MFMessageComposeViewController()
composeVC.messageComposeDelegate = self
// Configure the fields of the interface.
composeVC.recipients = ["000000000"]
var url = URL(string: self.getFileManager() as String)!
url.appendPathComponent("my_report.pdf")
composeVC.addAttachmentURL(url, withAlternateFilename:
"this file")
// Present the view controller modally.
if MFMessageComposeViewController.canSendText() {
self.present(composeVC, animated: true, completion: nil)
} else {
print("Can't send messages.")
}
}
解决方案
您使用了错误的 URL 初始化程序。URL(string:)
在这种情况下,初始化程序需要一个方案file://
。您需要使用URL(fileURLWithPath:)
初始化程序或简单地使用 FileManager urls 方法获取文档目录 URL:
extension URL {
static let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
}
let url = URL.documentDirectory.appendingPathComponent("my_report.pdf")
当您说“URL 是我的文件默认值中的 pdf 数据的路径”时,我不确定您的意思。如果您已将文件包含在项目 Bundle 中,则需要使用其url(forResource:)
方法。
let url = Bundle.main.url(forResource: "my_report", withExtension: "pdf")!
推荐阅读
- javascript - 吸气剂可以从 2 个不同的来源返回数据吗
- python - 如何使用电路python通过串行发送和接收
- android - New BLANK Project Android Studio ERROR:文件名、目录名或卷标语法不正确
- c# - 在 ASP.Net Core 2.0 Razor 中向 Html.Partial 传递附加参数
- batch-file - 如何创建一个批处理文件来生成多个批处理文件,其中包含命令?
- python - 在许多图中添加辅助轴
- javascript - Zapier/Javascript - 代码操作正在格式化
- android - React Native:带有 Android 和 Firebase 的 Chrome 调试器
- html - 当有多个按钮时单击一个按钮 vba
- php - php 通过 windows cmd - 多个实例