So basically I have been trying to create a very simple iMessage application where when a button is pressed, it inserts a message into the conversation a bit like what can be seen in this tutorial: The only issue I am having is that when the button is pressed, it runs the function however, nothing occurs as a result and I am really unsure as to why this is the case. I have been struggling with finding a working solution to this problem for a little while now and would really appreciate if someone would be kind enough to take a quick look at my code and perhaps point out where I went wrong or if I'm missing something. Any help on the topic will be greatly appreciated, thank you.
@IBAction func funcSend(_ sender: Any) {
let conversation = activeConversation
let session = conversation?.selectedMessage?.session
let layout = MSMessageTemplateLayout()
layout.caption = "Hi there"
var components = URLComponents()
let queryItem = URLQueryItem(name: "key", value: "value")
components.queryItems = [queryItem]
let message = MSMessage(session: session!)
message.layout = layout
message.url = components.url
message.summaryText = "Sent hi there message"
conversation?.insert(message)
}
It looks right, try to call dismiss() at the end?
Here's the code that works for me
@IBAction func onSend(_ sender: Any) {
print("sending...")
guard let conversation = activeConversation else { fatalError("Expected a conversation") }
guard let message = composeMessage(session: conversation.selectedMessage?.session)
else { return }
// Add the message to the conversation.
conversation.insert(message) { error in
if let error = error {
print(error)
}
}
// This one
dismiss()
}
And how I'm create the message
// MARK: Messaging
func composeMessage(session: MSSession? = nil) -> MSMessage? {
let layout = MSMessageTemplateLayout()
var components = URLComponents()
let caption = URLQueryItem(name: "caption", value: self.melody)
let decodedMelody = URLQueryItem(name: "melody", value: self.melody)
components.queryItems = [caption, decodedMelody]
let message = MSMessage(session: session ?? MSSession())
layout.image = self.screenImage.image
layout.caption = "Melody built with haptic and vibro."
layout.subcaption = "sent via iVibrio"
message.summaryText = "something summary"
if let conversation = activeConversation,
let msg = conversation.selectedMessage{
if msg.senderParticipantIdentifier == conversation.localParticipantIdentifier {
layout.caption = "$\(msg.senderParticipantIdentifier.uuidString) My msg"
}
else{
layout.caption = "$\(msg.senderParticipantIdentifier.uuidString) Edited msg"
}
}
message.url = components.url!
message.layout = layout
return message
}