问题描述
我想实现一个imessage应用程序,但是对于消息框架是新手,而iMessage应用程序是一个新东西,没有太多资源。所以我正在关注并使用提供以获取指南。
I want to implement an imessage app, however being new to the messages framework and iMessage apps being such a new thing there aren't many resources. So I am following the WWDC video and using Apples providing sample app for a guide.
我有三个视图, MessageViewController
可以处理几乎所有的功能,然后是 CreateViewController
和 DetailsViewController
。
I have three views, the MessageViewController
which handles pretty much all the functionality and then a CreateViewController
and a DetailsViewController
.
我只是想从 CreateViewController MSMessage
c>并显示在 DetailsViewController
..然后添加到数据。
I am simply trying to create an MSMessage
from the CreateViewController
and display in the DetailsViewController
.. then add to the data.
但是我在尝试时遇到了崩溃创建数据。
However I get a crash when trying to create the data.
@IBAction func createAction(_ sender: AnyObject) {
//present full screen for create list
self.delegate?.createViewControllerDidSelectAdd(self as! CreateViewControllerDelegate)
}
我试图传递的数据类型是结构中的字典:
The data type I am trying to pass is the dictionary from a struct:
struct data {
var title: String!
var date: Date!
var dictionary = ["title" : String(), "Array1" : [String](), "Array2" : [String]() ] as [String : Any]
}
所以这里是如何设置的;
So here's how things are set up;
MessagesViewController
class MessagesViewController: MSMessagesAppViewController, {
// MARK: Responsible for create list button
func composeMessage(for data: dataItem) {
let messageCaption = NSLocalizedString("Let's make", comment: "")
let dictionary = data.dictionary
func queryItems(dictionary: [String:String]) -> [URLQueryItem] {
return dictionary.map {
URLQueryItem(name: $0, value: $1)
}
}
var components = URLComponents()
components.queryItems = queryItems(dictionary: dictionary as! [String : String])
let layout = MSMessageTemplateLayout()
layout.image = UIImage(named: "messages-layout-1.png")!
layout.caption = messageCaption
let message = MSMessage()
message.url = components.url!
message.layout = layout
message.accessibilityLabel = messageCaption
guard let conversation = activeConversation else { fatalError("Expected Convo") }
conversation.insert(message) { error in
if let error = error {
print(error)
}
}
}
}
extension MessagesViewController: CreateViewControllerDelegate {
func createViewControllerDidSelectAdd(_ controller: CreateViewControllerDelegate) {
//CreatesNewDataItem
composeMessage(for: dataItem())
}
}
CreateViewController
/**
A delegate protocol for the `CreateViewController` class.
*/
protocol CreateViewControllerDelegate : class {
func createViewControllerDidSelectAdd(_ controller: CreateViewControllerDelegate)
}
class CreateViewController: UIViewController {
static let storyboardIdentifier = "CreateViewController"
weak var delegate: CreateViewControllerDelegate?
@IBAction func create(_ sender: AnyObject) {
//present full screen for create list
self.delegate?.createViewControllerDidSelectAdd(self as! CreateListViewControllerDelegate)
}
}
会不会有人显示我出错的地方以及如何发送 MSMessage
?如果我能够发送消息,那么我应该能够接收和重新发送。
Would someone show where I am going wrong and how I can send a MSMessage
? If I am able to send the message I should then be able to receive and resend.
推荐答案
我看到一个问题,我自己无法调试:
你要将components.queryItems设置为你的字典var转换为[String:String],但是从data.dictionary返回的字典不是[String:String],而是[String:Any]
One issue I see, without being able to debug this myself:you are setting your components.queryItems to your dictionary var cast as [String:String], but the dictionary returned from data.dictionary is not a [String:String], but a [String:Any]
特别是,dictionary [Array1]是一个字符串数组,而不是一个字符串。对于字典[Array2]也是如此。 URLQueryItem期望在其init()中给出两个字符串,但是你要尝试放入一个字符串和一个字符串数组(虽然我不确定你实际上是在queryItems中找到那一行(字典: )方法。
In particular, dictionary["Array1"] is an array of Strings, not a single string. Same for dictionary["Array2"]. URLQueryItem expects to be given two strings in its init(), but you're trying to put a string and an array of strings in (though I'm not sure that you're actually getting to that line in your queryItems(dictionary:) method.
当然,你的dataItem.dictionary返回一个包含4个空值的字典。我不确定你想要的是什么。
Of course, your dataItem.dictionary is returning a dictionary with 4 empty values. I'm not sure that's what you want.
这篇关于如何在消息扩展中发送MSMessage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!