0

This is my custom parameters path to add for dynamic link:

let parameters = "action=ORDER&codeId=1&goId=2"

This is how I create manual Dynamic Link:

let domain = "joytst.page.link"
let string = "https://\(domain)/?link=https://\(domain)/?\(parameters)"

and the output for shorten URL is:

"https://joytst.page.link/syjd9GjRH3SYCXFH7"

Then I am trying to create long url back from just shortened, and I got:

"https://joytst.page.link/?action=ORDER"

Where are codeId=1&goId=2 parameters?

3
  • "and the output for shorten URL is:" How do you get that output? You need to edit your question to include all relevant code in the form of a minimal reproducible example in order to make the question on-topic. Commented May 31, 2024 at 12:50
  • Shouldn't https://\(domain)/?\(parameters)" be percent escaped? Commented May 31, 2024 at 13:35
  • Do let components = URLComponents(string: string); print(components?.url?.absoluteString); components?.queryItems?.forEach({ print("\($0.name) : \($0.value)") }) in Swift. I guess it's not the way you would have expected it. Commented May 31, 2024 at 13:37

1 Answer 1

1

You currently have:

https://joytst.page.link/?link=https://joytst.page.link/?action=ORDER&codeId=1&goId=2

So let's decompose it with params and rename base1 and base2 the two joytst.page.link parts, else it's hard to separate them.

How it's really interpreted:

https://base1/?
               link=https://base2/?
                                   action=ORDER
              &codeId=1
              &goId=2

How you think it's interpreted:

https://base1/?
               link=https://base2/?
                                   action=ORDER
                                  &codeId=1
                                  &goId=2

You see that we can't guess what's the good interpretation from there, hence the rules and how it's done. The first one is the good interpretation of the URL and parameters.

In reality:
codeId=1 and goId=2 are params of base1 and action=ORDER is params of base2, that's why you get only action=ORDER because it doesn't know that codeId & goId are for link value.

You need to fully percent escape linkValue, that way it will have codeId and goId.

Your final url before shortening should be

https://joytst.page.link/?link=https://joytst.page.link/?action%3DORDER%26codeId%3D1%26goId%3D2

To illustrate it with your code:

let parameters = "action=ORDER&codeId=1&goId=2"
let domain = "joytst.page.link"
let string = "https://\(domain)/?link=https://\(domain)/?\(parameters)"

let components = URLComponents(string: string)
print(components!.url!.absoluteString)
components?.queryItems?.forEach({ print("\($0.name) : \($0.value)") })

Output:

$>https://joytst.page.link/?link=https://joytst.page.link/?action=ORDER&codeId=1&goId=2
$>link : Optional("https://joytst.page.link/?action=ORDER")
$>codeId : Optional("1")
$>goId : Optional("2")

Quick sample code with correctly percent escaping the full link value:

func constructURL(baseURL: String, with params: [URLQueryItem]) -> URL? {
    var components = URLComponents(string: baseURL)
    if !params.isEmpty {
        components?.queryItems = params
    }
    return components?.url
}



let domain = "joytst.page.link"
let embededQueryItems = [URLQueryItem(name: "action", value: "ORDER"),
                         URLQueryItem(name: "codeId", value: "1"),
                         URLQueryItem(name: "goId", value: "2")
]
let linkValue = constructURL(baseURL: "https://\(domain)/", with: embededQueryItems)

let finalURL = constructURL(baseURL: "https://\(domain)/", with: [URLQueryItem(name: "link", value: linkValue!.absoluteString)])
print(finalURL!.absoluteString)


let components = URLComponents(string: finalURL!.absoluteString)
print(components!.url!.absoluteString)
components?.queryItems?.forEach { 
    print("\($0.name) : \($0.value)")
}

Output:

$>https://joytst.page.link/?link=https://joytst.page.link/?action%3DORDER%26codeId%3D1%26goId%3D2
$>link : Optional("https://joytst.page.link/?action=ORDER&codeId=1&goId=2")
Sign up to request clarification or add additional context in comments.

3 Comments

Amazing answer. You worked a lot for it🤦🏻‍♂️ Thank you...
One more thing. It works for goId=1 but doesnt work for goId=2631b76e-9862-4ada-8d11-ff3d1c80d212. Every id is actually UUID string.
In my sample code it seems to work correctly as there are no character to escaped from the UUID value, and even if there was, you might need to escape them before...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.