简体   繁体   中英

Swift 3 - Sending message with image - Undetermined Literal

Currently trying to open MFMessageComposeViewController with an image attached but the typeIdentifier that I have found in older code seems to not be the right fit and I'm not able to find any documentation regarding attaching an image to a message other than copying the image to the PasteBoard/clipboard then having the user manually paste it in the message.

func sendMessageWith(imageData: Data) -> MFMessageComposeViewController? {
  if MFMessageComposeViewController.canSendText() == true {
      let composeVC = MFMessageComposeViewController()
      composeVC.messageComposeDelegate = self
      composeVC.addAttachmentData(imageData, typeIdentifier: kUTTypeJPEG, filename: "image.jpg")

      print("OK")
      return composeVC
    }

  print("Try Again")
  return nil
}

You need to import MobileCoreServices framework:

import MobileCoreServices

which contains the UTCoreTypes header which contains kUTTypeJPEG .

and you have to cast the constant to String because it's a CFString :

composeVC.addAttachmentData(
    imageData,
    typeIdentifier: kUTTypeJPEG as String,
    filename: "image.jpg"
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM