5

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.

I have three views, the MessageViewController which handles pretty much all the functionality and then a CreateViewController and a 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)

}

}

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.

halfer
  • 18,701
  • 13
  • 79
  • 158
JUSDEV
  • 711
  • 1
  • 6
  • 21
  • What might be the problem is passing the struct `dataItem` in the function `func composeMessage(for data: dataItem)` I have managed to send a string value without using a struct like in this video tutorial https://www.youtube.com/watch?v=pe-J7OsQHhI – RileyDev Sep 20 '16 at 22:39
  • @RileyDev - That youtube video helped! – KamyFC Jan 26 '17 at 16:47

1 Answers1

0

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]

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.

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

Jay Wardell
  • 2,105
  • 1
  • 12
  • 12
  • I have actually commented out QueryItems function and the dictionary, jus set the message manually but still get a runtime crash – JUSDEV Sep 21 '16 at 00:13
  • whats the text in the console when it crashes? What line does it stop at? – Jay Wardell Sep 21 '16 at 00:32
  • Is the code in your question up to date with your project? I don't see a createList() method on CreateViewController – Jay Wardell Sep 21 '16 at 00:40
  • Sorry, your line: self.delegate?.createViewControllerDidSelectAdd(self as! CreateListViewControllerDelegate) should probably read self.delegate?.createViewControllerDidSelectAdd(self as! CreateListViewController) since CreateListViewController is not its own delegate – Jay Wardell Sep 21 '16 at 00:43
  • and actually, you don't need the cast at all, just self.delegate?.createViewControllerDidSelectAdd(self) – Jay Wardell Sep 21 '16 at 00:45
  • Sorry `MessagesExtension`CreateViewController.create(AnyObject) -> ():` and that is exactly what I was trying but get the error `Value of type `CreateViewControllerDelegate` does not conform to expected type CreateViewControllerDelegate` – JUSDEV Sep 21 '16 at 00:49
  • send me the project via UPWORK and I'll try to sort it out. – Jay Wardell Sep 21 '16 at 00:50
  • I've submitted a proposal as Jay Wardell. I don't think I can message you until you message me. – Jay Wardell Sep 21 '16 at 00:56