Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To encode characters in a URL using Alamofire on iOS, you can use the URLComponents class provided by the Foundation framework. Here is an example:

import Alamofire

let urlString = "https://example.com/search?q=hello world"
guard let encodedUrlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
guard let url = URL(string: encodedUrlString) else { return }

Alamofire.request(url).responseJSON { response in
    // Handle response
}

In this example, we first encode the original URL string using the addingPercentEncoding(withAllowedCharacters:) method. We pass in the urlQueryAllowed character set to allow all characters that are allowed in a URL query.

Then, we create a URL instance from the encoded URL string. Finally, we use Alamofire to make a request to the URL and handle the response as needed.