Search code examples
iosswiftapiclean-urlsshort-url

Shorten URL in Swift iOS with your custom website domain name


I want to shorten a share URL I have in my app when users share a shop. I want a methodology or an API that allows me to shorten the URL to make it look clean but with my website's name.

for instance:

longURL: "https://example.com?x=somevalue&y=someothervalues"

Then when I shorten the URL with an API or method it would produce a small URL similar to this: "https://mywebsite.com/shortenedURLcode"

here is my code:

link = URL(string: "https://mywebsitename.com/share/?shop="+concatenate(getKey!,"&title="+shopName.text!.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!,"&desc="+summary.text!.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!,"&img="+profileImgURL))

I want to shorten the above link with my website's hostname.

How can I do that?


Solution

  • Here are two options to consider for shortening your URLs:

    Bitly API

    One option Bitly's API, which will allow you to make requests that return shortened links that fit the above mentioned criteria.

    It appears to achieve the result you seek, you must make two requests: one to receive the link with custom domain, and a second to customize the "back half" (See revenant API Documentation in hyperlinked text).

    The result of request #2 will contain your fully custom link.

    Rebrandly API

    The far better option in my opinion is Rebrandly. While I personally don't have any experience using any of Rebrandly's products, I would recommend this option as it has a generous free tier to get you started.

    And best of all, the entire shortened link can be formulated in a single request. Will note that what Bitly calls a "back-half" is referred to as the "slashtag" by Rebrandly, but these terms both refer to the exact same thing.

    API documentation can be found here.