Ok, I am working in an iMessage app and am trying to parse more than 1 url query item from the selected message here- I have been successful getting/sending just 1 value in a query:
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.
if(conversation.selectedMessage?.url != nil) //trying to catch error
{
let components = URLComponents(string: (conversation.selectedMessage?.url?.query?.description)!)
//let val = conversation.selectedMessage?.url?.query?.description
if let queryItems = components?.queryItems {
// process the query items here...
let param1 = queryItems.filter({$0.name == "theirScore"}).first
print("***************=> GOT IT ",param1?.value)
}
}
When I just have 1 value, just by printing conversation.selectedMessage?.url?.query?.description
I get an optional with that 1 value, which is good. But with multiple I cant find a clean way to get specific values by key.
What is the correct way to parse a URLQueryItem
for given keys for iMessage?
You can use iMessageDataKit library. It makes setting and getting data really easy and straightforward like:
(Disclaimer: I'm the author of
iMessageDataKit
)When you do
conversation.selectedMessage?.url?.query?.description
it simply prints out the contents of the query. If you have multiple items then it would appear something like:You can parse that one manually by splitting the string on "&" and then splitting the contents of the resulting array on "=" to get the individual key value pairs in to a dictionary. Then, you can directly refer to each value by key to get the specific values, something like this:
The above gives you an easy way to access the values by key. However, that is a bit more verbose. The way you provided in your code, using a filter on the
queryItems
array, is the more compact solution :) So you already have the easier/compact solution, but if this approach makes better sense to you personally, you can always go this route ...Also, if the issue is that you have to write the same filtering code multiple times to get a value from the
queryItems
array, then you can always have a helper method which takes two parameters, thequeryItems
array and aString
parameter (the key) and returns an optionalString
value (the value matching the key) along the following lines:Then your above code would look like: