好的,我正在构建一个 iMessage 应用程序,并且要来回传输数据,我必须使用URLQueryItem
s. 我正在使用一个SKScene
并且需要传输Int
s、CGPoint
s、图像等。阅读 Apple 的文档和我自己的尝试似乎你只能将字符串存储在URLQueryItem
s 中。
由于这是我们来回传递数据的唯一方法,是否有(更好的)方法来存储其他类型的数据?目前我一直在这样做:
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
再次将此字符串转换回。用 s 做这件事CGPoint
效率低下..如何通过 a CGPoint
in a之类的东西URLQueryItem
?除了将 x 和 y 值存储为字符串之外还有其他方法吗?
编辑:这就是我从另一个人那里接收数据并将其放入他们的场景的方式:
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!
}
}