I am making an iMessage Extension that involves users sending pictures back and forth to one another. They need to both be able to access the images that they receive from each other and use it on their own end. For example, if USER 1 sends a picture of a puppy to USER 2, the image property of the messages layout would be of the puppy. USER 2 should then be able to tap that message, and the puppy load into an image view on screen. So far I don't know how I would do this.
Here's where I set the layout image to that of a puppy.
@IBAction func sendPicturePressed(_ sender: AnyObject) {
if chosenImage.image != nil {
let session = MSSession()
let message = MSMessage(session: session)
let conversation = self.activeConversation
let components = URLComponents()
let layout = MSMessageTemplateLayout()
let image = chosenImage.image
layout.image = image
message.layout = layout
message.url = components.url!
conversation?.insert(message, completionHandler: { (error) in
self.dismiss()
})
}
}
Now when the second user taps the puppy, I want to set an image view on their screen to the puppy. Not exactly sure how, but what I'd LIKE to do is:
override func willBecomeActive(with conversation: MSConversation) {
imageView.image = conversation.selectedMessage.layout.image
//There is no image property to access this like I've provided, that's just what I'm trying to accomplish.
}
Although you cannot access the layout once message is received, you still have once chance to get it. In case extension is launched, message's layout is accessible on arrival in
-[didReceiveMessage:conversation:]
method of yourMSMessagesAppViewController
-based class.On the sender side you can assign your message a URL with custom ID (e.g. UUID) - it will be always accessible – then extract it on the receiver side in the
didReceiveMessage
together with the image and cache the latter locally with the ID as a name. Here's the code draft:After that, once message is tapped, the
-[didSelectMessage:conversation:]
will be called. Here you can get the URL again and read the corresponding image from cache (in case it is available):And don't forget to add all necessary checks for
nil
s, successfull URL creation etc.