Search code examples
iosswiftimessage

Store a variable in a conversation in iMessage Apps


I'm completly new to developing iMessage Apps.

I would like to store a shared variable (let's say an Int) to a conversation. The Use case could be an incrementing integer, that is incremented every time a message is send from any participant in the conversation.

Is this posible?

I have searched for this this without any luck.


Solution

  • You can attach shared variable in URL .

    //Insert the URL when user send the Message .

       let message = MSMessage()
        message.layout = layout
        message.url = URL(string: "application/sharedVariable=X")
        self.activeConversation?.insert(message, completionHandler: { (error) in
            print("error")
        })
    

    You can extract above shared Variable from above URL .

      let url = "http://application/sharedVariable=X" 
      let queryItems = URLComponents(string: url)?.queryItems
      let sharedValue = queryItems?.filter({$0.name == "sharedVariable"}).first          
      print(sharedValue?.value)
    

    If you do not want anyone to know the shared Variable then you can encrypt while sending.