2

Normally with MSMessage object setup, I would use on MSMessageTemplateLayout as the basic setup to send MSMessage objects. In the iMessage API, there's an interesting piece of code that allows interactive MSMessage called MSMessageLiveLayout where users can interact with the MSMessage object without fully opening up the MSMessage object.

For a demo, I tried to create a simple counter. For MSMessage objects, the only way that I know of to correctly save value changes is through the URL schemes.

The problem is that I'm not sure how to correctly update the current MSMessage URL to match the newly changes value. Also, how would I update the receiver end UI/Data of the MSMessage object to match the changes of the sender's?

I tried to access the current MSMessage URL by updating the current activeConversation url with a new URL value every time I pressed the counter button. The print statement shows that the URL updates. But, when I scroll up and down the MSMessage object the willBecomeActive function gets called - discarding any changes and continue to use the old URL value.



class Demo: MSMessagesAppViewController {

var session: MSSession?
var count = 1
@IBOutlet var label: UILabel!

   func createURL() -> URL {
        var urlComponents = URLComponents()
        urlComponents.scheme = "https";
        urlComponents.host = "www.democounter.com";

        let item = URLQueryItem(name: "counter", value: "\(count)")
        urlComponents.queryItems?.append(item)

        return urlComponents.url!
    }


    func createMessage(_ url: URL) {

        if session == nil {
            session = MSSession()
        }

        let message = MSMessage(session: session!)

        let layout = MSMessageTemplateLayout()
        layout.caption = "Sending counter"
        let liveLayout = MSMessageLiveLayout(alternateLayout: layout)

        message.layout = liveLayout
        message.url = url


        let conversation = self.activeConversation

        conversation?.insert(message, completionHandler: {(error) in
            if let error = error {
                print(error)
            }
        })

        self.dismiss()
    }

// this is the blue box in the example attached that create and sends 
   the MSMessage object

    @IBAction func sendMessage(_ sender: UIButton) {
          let url = createURL()
          createMessage(url)
    }

// counter button, this is where I try to update the current MSMessage URL

    @IBAction func PressMe(_ sender: UIButton) {

        count += 1
        print(count)
        label.text = String(count)

        let conversation = self.activeConversation
        print(conversation?.selectedMessage?.url)

      // It prints the changes, but does not save the updated url to 
      the current MSMessage Object

        conversation?.selectedMessage?.url = createURL()
        print(conversation?.selectedMessage?.url)

    }

    func decodeURL(_ url: URL) {

        let components = URLComponents(url: url,
                                       resolvingAgainstBaseURL: false)

        for (index, queryItem) in (components?.queryItems?.enumerated())! {

            if queryItem.name == "counter"  {
                count = Int(queryItem.value!)!
            }
        }
    }

    override func willBecomeActive(with conversation: MSConversation) {
        if let messageURL = conversation.selectedMessage?.url {
            decodeURL(messageURL)
            caption = "It's your turn to counter"
            session = conversation.selectedMessage?.session
        }
        label.text = String(count)   
    }
}


[Message Object Example] (https://i.stack.imgur.com/vCwxC.jpg)

SeeSawSin
  • 51
  • 2

0 Answers0