Search code examples
iosswiftstringurldeep-linking

How to get the redirect url from deeplink in Swift


How to get the URL from the given redirect deeplink string?

For example, if I have a deeplink string as

myapp://open-browser/https://mydaily.dev/JOURNAL/home?id=123e4567-e89b-12d3-a456-426655440000&source=01&channel_id=HOME

I would like to get the return result as

https://mydaily.dev/JOURNAL/home?id=123e4567-e89b-12d3-a456-426655440000&source=01&channel_id=HOME

I tried several methods from Apple documentation Accessing the Parts of a URL, the closest one was to combine path and query like below screenshot

Playground

Does anyone have a better way to do this?


Solution

  • I get your question, I think you want to get only the url starting from https://, please correct me.

    If so I think I could remove the deeplink url scheme myapp:// and the host open-browser/ like

    let url = "myapp://open-browser/https://mydaily.dev/JOURNAL/home?id=123e4567-e89b-12d3-a456-426655440000&source=01&channel_id=HOME"
    
    let deeplinkBase = "myapp://open-browser/"
    let newUrl = url.absoluteString.replacingOccurrences(of: deeplinkBase, with: "")
    

    Then you could use the newUrl as you like since we got the expected result now

    https://mydaily.dev/JOURNAL/home?id=123e4567-e89b-12d3-a456-426655440000&source=01&channel_id=HOME
    

    My example for the deeplinkBase string is hardcoded, but I guess you could use enums or any handlers for it later.