Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

encoding url using swift code

Tags:

url

ios

swift

nsurl

I need to send an URL in Arabic language, so I need to encode it before I put it in URL. I am using Swift code.

Below is an example what i really need

var s = "www.example.com/السلام عليكم"

let url = NSURL(string : s)

So the word (السلام عليكم) is in Arabic characters that what I want to send.

like image 660
inanva Avatar asked Apr 01 '15 18:04

inanva


People also ask

How do I encode a URL?

Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format. URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

What is encoding and decoding in Swift?

But before you can do that, you need to convert the data to a suitable format through a process called encoding or serialization. You'll also need to convert the saved data sent over the network to a suitable format before using it in your app. This reverse process is called decoding or deserialization.

What is URL in Swift?

URLs in Swift are used in a lot of ways. We fetch data using an API, images to visualize our app, and we often work with local files from our bundle. The Foundation framework allows us to access a lot of URL components easily with default parameters.


4 Answers

Swift 2.0

let urlwithPercentEscapes = myurlstring.stringByAddingPercentEncodingWithAllowedCharacters( NSCharacterSet.URLQueryAllowedCharacterSet())

Swift 3

let urlwithPercentEscapes = myurlstring.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)
like image 84
yarlg Avatar answered Oct 08 '22 03:10

yarlg


To improve @Druva's answer create an extention somewhere in the project

Swift 2.0

extension String
{   
    func encodeUrl() -> String
    {
        return self.stringByAddingPercentEncodingWithAllowedCharacters( NSCharacterSet.URLQueryAllowedCharacterSet())
    }
func decodeUrl() -> String
    {
        return self.stringByRemovingPercentEncoding
    }

}

Swift 3.0

 extension String
    {   
        func encodeUrl() -> String
        {
            return self.addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)
        }
    func decodeUrl() -> String
        {
            return self.stringByRemovingPercentEncoding
        }

    }
like image 44
Vyacheslav Avatar answered Oct 08 '22 04:10

Vyacheslav


You need to encode url as you have written. You can do so with that string method:

stringByAddingPercentEscapesUsingEncoding(NSStringEncoding)

So your code will be:

var s = "www.example.com/السلام عليكم"
// you may add check before force unwrapping
let url = NSURL(string : s.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)
like image 41
Vasily Avatar answered Oct 08 '22 04:10

Vasily


You need to encode this string as it contains special characters.

var s = "www.example.com/السلام عليكم"
let encodedLink = s.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)
let encodedURL = NSURL(string: encodedLink!)! as URL

where encodedURL is your final URL

like image 28
Gary G Avatar answered Oct 08 '22 03:10

Gary G