您可以在URLQueryItems中发送字符串以外的对象吗? [英] Can you send objects other than strings in URLQueryItems?

查看:136
本文介绍了您可以在URLQueryItems中发送字符串以外的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在构建一个iMessage应用程序,并且要来回传输数据,我必须使用URLQueryItem s.我正在使用SKScene,并且需要传输IntCGPoint,图像等.阅读Apple的文档和我自己的尝试似乎只能将字符串存储在URLQueryItem中.

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

然后在反面,我必须再次将此字符串转换回Int.用CGPoint执行此操作效率不高..如何在URLQueryItem中传递类似CGPoint的内容?除了将x和y值存储为字符串之外,还有其他方法吗?

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?

这就是我从另一个人那里接收数据并将其放入他们的场景的方式:

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

推荐答案

如您所见,要通过URLQueryItem传递数据,您必须将所有内容转换为String s,因为该信息应该表示为毕竟是URL :)对于CGPoint信息,您可以将x和y值分开,并将它们作为转换为String的两个单独的Int发送.或者,您可以将其作为单个字符串值以"10,5"的形式发送,其中10是x,而5是y值,但在另一端,您需要先在逗号上分割该值,然后进行转换结果值返回到Int s,就像这样(在另一端):

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

对于其他类型的数据,您必须遵循类似的策略,以某种方式将值转换为String.对于图像,如果资源中有图像,则只需传递名称或标识号就可以逃脱.对于外部图像,URL(如果图像全部来自同一服务器,则为URL的一部分)应该起作用.否则,您可能必须考虑对图像数据进行base64编码或使用URLQueryItem进行某种编码,但是到了这一点,您可能希望查看要实现的目标,并且也许有更好的方法可以这样做是因为大图像可能会导致发送大量数据,而且我不确定iMessage应用程序是否支持该功能.因此,您可能还想研究iMessage应用程序数据传递中的限制.

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.

希望这会有所帮助:)

这篇关于您可以在URLQueryItems中发送字符串以外的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆