简体   繁体   中英

How to convert string to URL in swift

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

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.

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.

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!)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM