1

I want to convert below String to URL

www.mydomain.com/key=अक्षय

I tried let urlToSend = URL(string: "www.mydomain.com/key=अक्षय")! but it returns nil.

I'm getting that 'अक्षय' keyword from textField, it could be in any local language.

It works fine with English but not working with local language.

I used let url = URL(string: "www.mydomain.com")?.appendingPathComponent("key=अक्षय") it gives www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF

but now I want to www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF to www.mydomain.com/key=अक्षय

Kathiresan Murugan
  • 2,783
  • 3
  • 23
  • 44
Akshay Phulare
  • 1,359
  • 2
  • 10
  • 15
  • Possible duplicate of [Swift - encode URL](https://stackoverflow.com/questions/24551816/swift-encode-url) – Nazmul Hasan Jan 03 '18 at 10:27
  • here is your answer : https://stackoverflow.com/a/43682753/4415445 – Nazmul Hasan Jan 03 '18 at 10:29
  • "www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF" is the valid url. Please explain what you mentioned "but now I want to www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF to www.mydomain.com/key=अक्षय"... You could also check [my answer](https://stackoverflow.com/a/48075721/5501940). – Ahmad F Jan 03 '18 at 10:45

5 Answers5

4

You need to encode the URL.

let urlString = "www.mydomain.com/key=अक्षय".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)
let url = URL(string: urlString!)

let decodedUrl = urlString?.removingPercentEncoding

Keep in mind that you shouldn't force unwrap URL's and strings, use if let or guard statements.

Oliver Kulpakko
  • 105
  • 3
  • 8
4

When working with URLs as strings, you should encode them to be valid as a URL; One of the most popular examples of encoding the URL is that the " " (space) would be encoded as "%20".

So in your case the encoded value of your url should be:

www.mydomain.com%2Fkey=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF

As you noticed the value of the key is changed

from: "अक्षय"

to:"%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF"

which will let the URL to be valid.

How to:

you could get the above result like this:

let string = "www.mydomain.com/key=अक्षय"

if let encodedString  = string.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), let url = URL(string: encodedString) {
    print(url) // www.mydomain.com%2Fkey=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF
}

Note that there is optional binding for both the encoded string and the url for the purpose of being safe.

Decoding the URL:

You could also returns to the original unencoded url (decoding it) like this:

let decodedString = "www.mydomain.com%2Fkey=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF"

if let unwrappedDecodedString = decodedString.removingPercentEncoding {
    print(unwrappedDecodedString) // www.mydomain.com/key=अक्षय
}

Again, optional binding to be safe.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • but I want to back 'www.mydomain.com%2Fkey=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF' to 'www.mydomain.com/key=अक्षय' – Akshay Phulare Jan 03 '18 at 10:46
  • 1
    @AkshayPhulare to make sure that I got it, you want to let "www.mydomain.com%2Fkey=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF" to be decoded to "www.mydomain.com/key=अक्षय", is it correct? – Ahmad F Jan 03 '18 at 10:49
2

Try this thing:

    let strURL = "www.mydomain.com/key=अक्षय".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
    let url = URL(string: strURL!)

Or you can use:

let strURL = "www.mydomain.com/key=अक्षय".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)
let url = URL(string: strURL!)
Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
1

Instead of dealing with percent encodings explicitly, you can also build the URL piece by piece, using appendingPathComponent:

let url = URL(string: "www.mydomain.com")?.appendingPathComponent("key=अक्षय")
// www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF
Gereon
  • 17,258
  • 4
  • 42
  • 73
  • but I want to back 'www.mydomain.com%2Fkey=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7‌​%E0%A4%AF' to 'www.mydomain.com/key=अक्षय' – Akshay Phulare Jan 03 '18 at 10:48
  • Not sure what you mean by "I want to back" exactly. If you're talking about reversing the percent encodings, try `.removePercentEncoding` on `url.absoluteString`. – Gereon Jan 03 '18 at 10:52
0

Swift3.0

let baseUrl = "www.mydomain.com/key=अक्षय" // base url

let encodedUrl : String! = baseUrl.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) // remove the spaces in the url string

let typeUrl = URL(string: encodedUrl)! // convert the string into url

print(typeUrl)  // www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF

For Checking purpose

if let unwrappedDecodedString = encodedUrl.removingPercentEncoding {
        print(decodedString) // www.mydomain.com/key=अक्षय
}
Kathiresan Murugan
  • 2,783
  • 3
  • 23
  • 44