正在插入消息,iMessage 应用 swift 3
Inserting message, iMessage application swift 3
所以基本上我一直在尝试创建一个非常简单的 iMessage 应用程序,当按下一个按钮时,它会在对话中插入一条消息,有点像在本教程中看到的那样:我遇到的唯一问题是当按下按钮时,它会运行该功能,但是结果什么也没有发生,我真的不确定为什么会这样。一段时间以来,我一直在努力寻找解决这个问题的有效方法,如果有人愿意快速查看我的代码并指出我哪里出错或者我遗漏了什么,我将不胜感激.非常感谢有关该主题的任何帮助,谢谢。
@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)
}
看起来不错,试试最后调用dismiss()?
这是适合我的代码
@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()
}
以及我是如何创建消息的
// 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
}
所以基本上我一直在尝试创建一个非常简单的 iMessage 应用程序,当按下一个按钮时,它会在对话中插入一条消息,有点像在本教程中看到的那样:我遇到的唯一问题是当按下按钮时,它会运行该功能,但是结果什么也没有发生,我真的不确定为什么会这样。一段时间以来,我一直在努力寻找解决这个问题的有效方法,如果有人愿意快速查看我的代码并指出我哪里出错或者我遗漏了什么,我将不胜感激.非常感谢有关该主题的任何帮助,谢谢。
@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)
}
看起来不错,试试最后调用dismiss()?
这是适合我的代码
@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()
}
以及我是如何创建消息的
// 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
}