iMessage App: Sending Audio Files

Apple’s documentation is quite misleading.

Vishal V. Shekkar
Vishal V. Shekkar
4 min readJan 13, 2017

--

Broadly speaking, an iMessage App or extension can send a message in two ways — a message with an MSMessageTemplateLayout object or as an attachment. The attachment just sends the file without any descriptors for the iMessage App or metadata. The message with a MSMessageTemplateLayout object specified can be customized.

MSMessageTemplateLayout

MSMessageTemplateLayout has a property called mediaFileURL which accepts a file URL for a locally-stored media file.

As seen in the description of mediaFileURL on the screenshot from Xcode, the media could be a video or an audio file. For the audio file, the description says, that a graphical representation of the waveform would be shown.

So, I set this property with the file URL for an .m4a file in my iMessage Extension and iOS App’s common app group container. When I sent the message, the message bubble did not contain any audio. It was a very plain bubble with just the caption being shown next to the app icon as seen below.

Attempt to send an audio file with MSMessageTemplateLayout

But, the file got sent when I used the insertAttachment:withAlternateFilename:completionHandler: method on the active conversation object, as seen in the image below.

Audio file sent with insertAttachment method.

Using the attachment method will send the file over, but, it does not show any app-related content. My app’s icon is not shown, and there is no way to add captions or other metadata to the media that’s being sent.

What the Documentation Has Got to Say

Skeleton of a customizable MSMessageTemplateLayout object.

The image above is picked from Apple’s documentation, and even there, they mention that an audio file can be added to a MSMessageTemplateLayout object.

With a determination to get this working, I rechecked my app group container identifier, checked if I was using the proper URL scheme for the file, checked with different audio formats ranging from .mp3 to .aac in addition to my initial .m4a. Nothing worked. Then, I started reading more into the documentation to see if I was doing something else wrong.

I went to see the documentation of mediaFileURL property in MSMessageTemplateLayout class on Apple’s developer website. Even there, it’s mentioned that audio files do work. See image below.

Documentation on mediaFileURL

Since the documentation mentioned audio files as being accepted very clearly, I was quite convinced that I missed something very simple. I often miss things hiding in plain sight. I then read a lot of Stack Overflow questions and then realised that it’s possible that this is a bug from Apple. It’s more of a missing feature that was advertised than a bug.

Then, I went in to see what was written about mediaFileURL where it was declared. It’s a very different story here.

If you didn’t catch it from the image,

…the data contained in this file must conform to kUTTypeImage or kUTTypeMovie.

It says that the data in the file must either be that of an image or a movie, and not of an audio stream. Get over here to see what each Uniform Type Identifier entails.

Conclusion

It’s possible that I’m still missing something. But, there’s definitely some inconsistency in the documentation and between what was advertised and what works. This ordeal took up a couple of hours of my time.

Check out this Stack Overflow thread for some more info.

If anybody finds a way to send audio files without losing the custom bubble, please write a response with your solution.

I write about everything I find interesting and worthy enough to share.
♥︎ this article if you feel more people should read it and follow me and my blog to read my upcoming stories.

You can follow me on Twitter here.

--

--