MFMessageComposeViewController iOS7 addAttachmentData:typeIdentifier:filename: not working

此生再无相见时 提交于 2019-11-28 05:02:06

The MFMessageComposeViewController wants the attachment to have the correct extension for the type of image you're uploading. I verified by testing with a PNG file, and the following variations of adding the attachment data:

[messageController addAttachmentData:imgData typeIdentifier:@"public.data" filename:@"image"];
[messageController addAttachmentData:imgData typeIdentifier:@"public.data" filename:@"image.abc"];
[messageController addAttachmentData:imgData typeIdentifier:@"public.data" filename:@"image.png"];

Only the last option worked. I didn't need to change the typeIdentifier, although it probably would make sense to choose a UTI that matches the type of data.

The full list of UTIs is available here: System-Declared Uniform Type Identifiers (Thanks @iWasRobbed!)

For Swift You can try this

if (MFMessageComposeViewController.canSendText()) {

     let controller = MFMessageComposeViewController()
     controller.body = "Solution for broken image in composer"
     controller.messageComposeDelegate = self

      if image.imageAsset != nil {

            let imageData = UIImageJPEGRepresentation(self.fixOrientation(img: image), 1)  //! as NSData
            controller.addAttachmentData(imageData! , typeIdentifier: "image/.jpeg", filename: "image.jpeg")

        }

        viewController.present(controller, animated: true,completion: {

        })

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!