Search code examples
swiftswift3imessage-extension

Can you send objects other than strings in URLQueryItems?


Ok, I am building an iMessage app and to transfer data back and forth I have to use URLQueryItems. I am working with an SKScene and need to transfer Ints, CGPoints, images, etc. Reading Apple's documentation and my own attempts it seems like you can only store strings in URLQueryItems.

As this us the only way to pass data back and forth, is there a (better) way to store other types of data? Currently I have been doing this:

 func composeMessage(theScene: GameScene) {
        let conversation = activeConversation
        let session = conversation?.selectedMessage?.session ?? MSSession()

        let layout = MSMessageTemplateLayout()
        layout.caption = "Hello world!"

        let message = MSMessage(session: session)
        message.layout = layout
        message.summaryText = "Sent Hello World message"

        var components = URLComponents()
        let queryItem = URLQueryItem(name: "score",value: theScene.score.description)
       components.queryItems = [queryItem] //array of queryitems
        message.url = components.url!

        print("SENT:",message.url?.query)

        conversation?.insert(message, completionHandler: nil)
    }

Then on the flip side I have to convert this string back to an Int again. Doing this with CGPoints will be inefficient.. how would one pass something like a CGPoint in a URLQueryItem? Any other way than storing the x and y values as strings?

EDIT: This is how I have been receiving data from the other person and putting into their scene:

override func willBecomeActive(with conversation: MSConversation) {
        // Called when the extension is about to move from the inactive to active state.
        // This will happen when the extension is about to present UI.

        // Use this method to configure the extension and restore previously stored state.

        let val = conversation.selectedMessage?.url?.query?.description
         print("GOT IT ", val)

        if(val != nil)
        {
            scene.testTxt = val!
        }
    }

Solution

  • As you discovered, to pass data via URLQueryItem, you do have to convert everything to Strings since the information is supposed to be represented as a URL after all :) For CGPoint information, you can break the x and y values apart and send them as two separate Ints converted to String. Or, you can send it as a single String value in the form of "10,5" where 10 is the x and 5 is the y value but at the other end you would need to split the value on a comma first and then convert the resulting values back to Ints, something like this (at the other end):

    let arr = cgPointValue.components(separatedBy:",")
    let x = Int(arr[0])
    let y = Int(arr[1])
    

    For other types of data, you'd have to follow a similar tactic where you convert the values to String in some fashion. For images, if you have the image in your resources, you should be able to get away with passing just the name or an identifying number. For external images, a URL (or part of one if the images all come from the same server) should work. Otherwise, you might have to look at base64 encoding the image data or something if you use URLQueryItem but if you come to that point, you might want to look at what you are trying to achieve and if perhaps there is a better way to do it since large images could result in a lot of data being sent and I'm not sure if iMessage apps even support that. So you might want to look into limitations in the iMessage app data passing as well.

    Hope this helps :)