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=अक्षय

3
  • Possible duplicate of Swift - encode URL Commented Jan 3, 2018 at 10:27
  • here is your answer : stackoverflow.com/a/43682753/4415445 Commented Jan 3, 2018 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. Commented Jan 3, 2018 at 10:45

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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=अक्षय'
Updated my answer.
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.

2 Comments

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=अक्षय'
@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?
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!)

2 Comments

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=अक्षय'
Just add strURL.removingPercentEncoding and again you will get your source string
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

2 Comments

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=अक्षय'
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.
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=अक्षय
}

Comments

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.