Swift 5.0版本: 调用以下名为displayMessageInterface
的方法:
-重要说明:
-
composeViewController.addAttachmentData(dataImage!, typeIdentifier: "image/png", filename: "ImageData.png")
在上一行中,对于我来说,文件名的类型必须为
abc.png
,如果使用的是jpeg图片数据,则文件名必须为abc.jpeg
,并且typeIdentifier必须遵循image/png
和image/jpeg
分别。 我很难找到答案。即使其他答案已经足够,我也写这个答案的原因。
有关typeIdentifiers的更多信息,请使用以下链接:https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1
fileprivate func displayMessageInterface() {
if MFMessageComposeViewController.canSendText() {
let composeViewController = MFMessageComposeViewController()
composeViewController.messageComposeDelegate = self
composeViewController.body = "Enter your text body here."
if MFMessageComposeViewController.canSendAttachments() {
let image = UIImage(named: "image-name")!
let dataImage = image.pngData()
guard dataImage != nil else {
return
}
composeViewController.addAttachmentData(dataImage!, typeIdentifier: "image/png", filename: "ImageData.png")
}
self.present(composeViewController, animated: true)
} else {
print("Can't send messages.")
}
}
由于我在上面的方法中提到了委托,因此在UIViewController
的情况下可以使用这种方式:
extension UIViewController: MFMessageComposeViewControllerDelegate {
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
if result == .failed {
print("could not send message")
}
self.dismiss(animated: true)
}
}
网友评论