3
\$\begingroup\$

How should I decode escaped URL by unity in Swift?

I am developing a game with Unity3d. Something MMORPG. With server implemented by me in Swift under Kitura framework A framework doesn't matter that much. It is more about swift and Unity itself.

Let me describe the problem here.

Unity

UnityWebRequest.EscapeURL("First Last")

Escapes string with encoding used in the application/x-www-form-urlencoded content-type: https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1 Which produce string First+Last where + maps to " " space.

Swift:

import Foundation


var str = "First+Last"
print(str)
print(str.removingPercentEncoding)

The character used on this string + isn't considered as a percentage encoding in the standard used by swift. It would be if the character escaped as this would be represented by %20.

So my server and client ended up with different string values. Later on returned from one to the other weren't equal. Which ended up considering them as two different clients.

enter image description here

\$\endgroup\$
1
\$\begingroup\$

Reason: Unity uses a different standard to escapes URLs then swift one.

Escapes string with encoding used in the application/x-www-form-urlencoded content-type: https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1 Which produce string First+Last where + maps to " " space.

Possible solution: Is to replace "+" by yourself with " " before attempting to encode URL in swift.

"Encoded+String%2B".replacingOccurrences(of: "+", with: " ").removingPercentEncoding
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.