问题描述
我在我的应用程序中具有消息发送功能,并使用MFMessageComposeViewController实现了该功能.我可以在iOS9中将照片与消息一起附加,但在iOS 10中不能吗?有人遇到同样的问题吗?
I have message send functionality in my app and implemented the same using MFMessageComposeViewController. I am able to attach photos with the message in iOS9 but not in iOS 10? Is there anyone having the same issue?
推荐答案
Swift 5.0版本:调用以下名为 displayMessageInterface
的方法:
Swift 5.0 version:Call the below method named displayMessageInterface
:
-重要说明:
-
composeViewController.addAttachmentData(dataImage !, typeIdentifier:"image/png",文件名:"ImageData.png")
在上一行中,对于我来说,文件名的类型必须为 abc.png
,如果使用的是jpeg图片数据,则文件类型必须为 abc.jpeg
,并且typeIdentifier必须遵循 image/png
和 image/jpeg
.我很难找到答案.即使其他答案已经足够,我也写这个答案的原因.
In the above line, filename must be of type abc.png
in my case or abc.jpeg
if you are using a jpeg image data and typeIdentifier must follow image/png
and image/jpeg
respectively. I struggled a lot to find out this. The reason I write this answer even when other answers are enough already.
有关typeIdentifiers的更多信息,请使用以下链接: https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1
For more information on typeIdentifiers, use this link: 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
的情况下,可以使用这种方式:
Since I have mentioned the delegate in the above method, you can use it this way in case of a UIViewController
:
extension UIViewController: MFMessageComposeViewControllerDelegate {
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
if result == .failed {
print("could not send message")
}
self.dismiss(animated: true)
}
}
这篇关于在ios10中禁用了使用MFMessageComposeViewController发送照片的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!