Search code examples
iosswiftswift3sprite-kitimessage-extension

SKScene and URLQueryItems in Swift3?


Ok, I am new to URL querying and this whole aspect of Swift and need help. As is, I have an iMessage app that contains and SKScene. For the users to take turns playing the game, I need to send the game back and forth in messages within 1 session as I learned here : https://medium.com/lost-bananas/building-an-interactive-imessage-application-for-ios-10-in-swift-7da4a18bdeed.

So far I have my scene all working however Ive poured over Apple's ice cream demo where they send the continuously-built ice cream back and forth, and I cant understand how to "query" everything in my SKScene so I can send the scene.

I'm unclear as to how URLQueryItems work as the documentation does not relate to sprite kit scenes.

Apple queries their "ice cream" in its current state like this:

    init?(queryItems: [URLQueryItem]) {
            var base: Base?
            var scoops: Scoops?
            var topping: Topping?

            for queryItem in queryItems {
                guard let value = queryItem.value else { continue }

                if let decodedPart = Base(rawValue: value), queryItem.name == Base.queryItemKey {
                    base = decodedPart
                }
                if let decodedPart = Scoops(rawValue: value), queryItem.name == Scoops.queryItemKey {
                    scoops = decodedPart
                }
                if let decodedPart = Topping(rawValue: value), queryItem.name == Topping.queryItemKey {
                    topping = decodedPart
                }
            }

            guard let decodedBase = base else { return nil }

            self.base = decodedBase
            self.scoops = scoops
            self.topping = topping
        }
    }

fileprivate func composeMessage(with iceCream: IceCream, caption: String, session: MSSession? = nil) -> MSMessage {
        var components = URLComponents()
        components.queryItems = iceCream.queryItems

        let layout = MSMessageTemplateLayout()
        layout.image = iceCream.renderSticker(opaque: true)
        layout.caption = caption

        let message = MSMessage(session: session ?? MSSession())
        message.url = components.url!
        message.layout = layout

        return message
    }
}

But I cant find out how to "query" an SKScene. How can I "send" an SKScene back and forth? Is this possible?


Solution

  • You do not need to send an SKScene back and forth :) What you need to do is send the information relating to your game set up - such as number of turns, or whose turn it is, or whatever, as information that can be accessed by your app at the other end to build the scene.

    Without knowing more about how your scene is set up and how it interacts with the information received for the other player's session, I can't tell you a lot in terms of specifics. But, what you need to do, if you are using URLQueryItems to pass the information, simply retrieve the list of query items in your scene and set up the scene based on the received values.

    If you have specific questions about how this could be done, if you either share the full project, or post the relevant bits of code as to where you send out a message from one player and how the other player receives the information and sets up the scene, I (or somebody else) should be able to help.

    Also, if you look at composeMessage in the code you posted above, you will see how in that particular code example the scene/game information was being sent to the other user. At the other end of the process, the received message's URL parameter would be decomposed to get the values for the various query items and then the scene would be set up based on those values. Look at how that is done in order to figure out how your scene should be set up.