How to send MSMessage to message extension?

I want to implement the imessage application, but being new to the message structure and iMessage applications is such a new thing that there are not many resources. So I follow the WWDC video and using Apples, providing an example application for guidance.

I have three kinds: MessageViewController , which handles almost all the functionality, and then CreateViewController and DetailsViewController .

I'm just trying to create MSMessage from CreateViewController and display in DetailsViewController .. then add to the data.

However, when I try to create data, I get a failure.

 @IBAction func createAction(_ sender: AnyObject) { //present full screen for create list self.delegate?.createViewControllerDidSelectAdd(self as! CreateViewControllerDelegate) } 

The data type I'm trying to pass is a dictionary from a struct:

 struct data { var title: String! var date: Date! var dictionary = ["title" : String(), "Array1" : [String](), "Array2" : [String]() ] as [String : Any] } 

So how is everything arranged;

MessagesViewController

 class MessagesViewController: MSMessagesAppViewController, { // MARK: Responsible for create list button func composeMessage(for data: dataItem) { let messageCaption = NSLocalizedString("Let 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) } } 

Someone show where I am wrong, and how can I send MSMessage ? If I can send a message, I must receive and resend it.

+5
source share
1 answer

One of the questions that I see, having no way to debug this myself: you install your .queryItems components in your var cast dictionary as [String: String], but the dictionary returned from data.dictionary is not [String: String], and [String: Any]

In particular, the dictionary ["Array1"] is an array of strings, not a single string. The same goes for the dictionary ["Array2"]. URLQueryItem expects to be given two strings in init (), but you are trying to put a string and an array of strings (although I'm not sure if you really get into that string in your queryItems (dictionary :).

Of course, your dataItem.dictionary returns a dictionary with 4 empty values. I'm not sure what you want.

0
source

All Articles