Can you send objects other than strings in URLQueryItems?

廉价感情. 提交于 2020-01-14 03:46:16

问题


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!
        }
    }

回答1:


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 :)




回答2:


You can use iMessageDataKit library for storing key-value pairs in your MSMessage objects. It makes setting and getting data really easy and straightforward like:

let message: MSMessage = MSMessage()

message.md.set(value: 7, forKey: "moveCount")
message.md.set(value: "john", forKey: "username")
message.md.set(values: [15.2, 70.1], forKey: "startPoint")
message.md.set(values: [20, 20], forKey: "boxSize")

if let moveCount = message.md.integer(forKey: "moveCount") {
    print(moveCount)
}
if let username = message.md.string(forKey: "username") {
    print(username)
}
if let startPoint = message.md.values(forKey: "startPoint") {
    print("x: \(startPoint[0])")
    print("y: \(startPoint[1])")
}
if let boxSize = message.md.values(forKey: "boxSize") {
  let size = CGSize(width: CGFloat(boxSize[0] as? Float ?? 0),
                    height: CGFloat(boxSize[1] as? Float ?? 0))
  print("box size: \(size)")
}

(Disclaimer: I'm the author of iMessageDataKit)



来源:https://stackoverflow.com/questions/43334841/can-you-send-objects-other-than-strings-in-urlqueryitems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!