Sivaram Yadav
Sivaram Yadav

Reputation: 3333

Convert the following urlString to encoded urlString in Swift 3.0

The following URL need to be encode in Swift 3

https://maps.googleapis.com/maps/api/geocode/json?address=madhapur.%20Hyderabad

Upvotes: 1

Views: 190

Answers (2)

Nirav D
Nirav D

Reputation: 72410

You can encode and decode the URL like this.

let stringURL = "https://maps.googleapis.com/maps/api/geocode/json?address=madhapur Hyderabad"
if let encodeURL = stringURL.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
    print(encodeURL)
    let decodeURL = encodeURL.removingPercentEncoding
    print(decodeURL)
}

Output

https://maps.googleapis.com/maps/api/geocode/json?address=madhapur%20Hyderabad
Optional("https://maps.googleapis.com/maps/api/geocode/json?address=madhapur Hyderabad")

Upvotes: 1

Ajay Singh Mehra
Ajay Singh Mehra

Reputation: 1391

You can use removingPercentEncoding

var str = "https:%2F%2Fmaps.googleapis.com%2Fmaps%2Fapi%2Fgeocode%2Fjson%3Faddress=madhapur.%20Hyderabad"
str = str.removingPercentEncoding!

Result:- https://maps.googleapis.com/maps/api/geocode/json?address=madhapur. Hyderabad

Upvotes: 0

Related Questions