1

I am making a iMessage game. I want only the current iMessage session to show and all of the previous ones to be minimized. For a turn based game I don't want users to go back and see all the previous moves.

How might this be done with iOS 10 and Swift?

quemeful
  • 8,148
  • 4
  • 51
  • 64
  • @RashwanL I was posting the question to answer myself, because since iMessage extensions are very new, there is a lack of resources on the web – quemeful Sep 20 '16 at 20:38
  • @quemeful Could you check out this question I have been trying to do the same thing http://stackoverflow.com/questions/39600081/how-to-send-msmessage-in-messages-extension – A.Roe Sep 20 '16 at 21:33
  • @matt Forgive me for specifying what platform and language I was using. If you notice, I was asking this question to answer myself to help all the people like me who prefer SO answers over Apple's hard to use documentation – quemeful Sep 26 '16 at 11:42

1 Answers1

3

You want to use a single MSSession across all the messages in the conversation.

1. The session variable (declared in the main class)

class MessagesViewController: MSMessagesAppViewController {
    var session: MSSession?

    ...
}

2. Setting the session (when the app becomes active)

override func willBecomeActive(with conversation: MSConversation) {
    if let selected = conversation.selectedMessage {
        session = selected.session
    }
}

3. Use the session (when building the MSMessage)

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

let message = MSMessage(session: session!)

Full Code Example

class MessagesViewController: MSMessagesAppViewController {
    var session: MSSession?

    override func willBecomeActive(with conversation: MSConversation) {
        if let selected = conversation.selectedMessage {
            session = selected.session
        }
    }

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

        let message = MSMessage(session: session!)

        ...
    }
}
quemeful
  • 8,148
  • 4
  • 51
  • 64