How to send GIF Image through iMessage extension

369 views Asked by At

I would like to send a GIF Image from my iMessage extension. The message is sending with the image but the problem is that image is not animating.

My approach:

    let msg = msgList[indexPath.row]
    
    let message = MSMessage()
    let msgLayout = MSMessageTemplateLayout()
    
    if let title = msg.message { msgLayout.caption = title }
    if let msgImg = msg.image { msgLayout.image = msgImg }
    
    message.layout = msgLayout

    if let conversation = self.activeConversation {
            conversation.insert(message) { error in
                print("Insert message error: \(String(describing: error))")
           }
     }

Also tried with mediaFileUrl:

 if let url = Bundle.main.url(forResource: "IMG_0673", withExtension: "gif") {
            msgLayout.mediaFileURL = url
        }

But when I tried with MSSticker GIF is animating.

Code:

    do {
        let sticker = try MSSticker(contentsOfFileURL: Bundle.main.url(forResource:"AS001494_20", withExtension: "gif")!,localizedDescription: "a gif image")
        
        if let conversation = self.activeConversation {
            conversation.insert(sticker) { error in
                print(error)
            }
        }
        
    } catch {
        print("Sticker error: \(error.localizedDescription)")
    }

Is it possible to send animated GIF using MSMessage and MSMessageTemplateLayout?

1

There are 1 answers

0
AcidicSkittles On

You may need to use insertAttachment(_:withAlternateFilename:completionHandler:) and send the GIF as an attachment. I also could not get GIFs to animate with MSMessageTemplateLayout. So you'll have something like

let mediaURL = Bundle.main.url(forResource: "IMG_0673", withExtension: "gif")!
if let conversation = self.activeConversation {
    conversation.insertAttachment(mediaURL, withAlternateFilename: nil) { error in
        print("Insert attachment message error: \(String(describing: error))")
    }
}

Happy coding