I am using AFNetworking in my iOS app and for all the GET requests it makes, I build the url from a base URL and than add parameters using NSDictionary Key-Value pairs.
The problem is that I need same key for different values.
Here is an example of what I need the finally URL to look like -
http://example.com/.....&id=21212&id=21212&id=33232
It's not possible in NSDictionary to have different values in same keys. So I tried NSSet but did not work.
let productIDSet: Set = [prodIDArray] let paramDict = NSMutableDictionary() paramDict.setObject(productIDSet, forKey: "id")
Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".
Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.
Although officially there is no limit specified by RFC 2616, many security protocols and recommendations state that maxQueryStrings on a server should be set to a maximum character limit of 1024.
All you need is URLComponents
(or NSURLComponents in Obj-C). The basic idea is to create a bunch of query items for your id's. Here's code you can paste into a playground:
import Foundation import XCPlayground let queryItems = [URLQueryItem(name: "id", value: "1"), URLQueryItem(name: "id", value: "2")] var urlComps = URLComponents(string: "www.apple.com/help")! urlComps.queryItems = queryItems let result = urlComps.url! print(result)
You should see an output of
www.apple.com/help?id=1&id=2
It can add the QueryItem to your existing URL.
extension URL { func appending(_ queryItem: String, value: String?) -> URL { guard var urlComponents = URLComponents(string: absoluteString) else { return absoluteURL } // Create array of existing query items var queryItems: [URLQueryItem] = urlComponents.queryItems ?? [] // Create query item let queryItem = URLQueryItem(name: queryItem, value: value) // Append the new query item in the existing query items array queryItems.append(queryItem) // Append updated query items array in the url component object urlComponents.queryItems = queryItems // Returns the url from new url components return urlComponents.url! } }
How to use
var url = URL(string: "https://www.example.com")! let finalURL = url.appending("test", value: "123") .appending("test2", value: nil)
In this method, the URL will be updated automatically.
extension URL { mutating func appendQueryItem(name: String, value: String?) { guard var urlComponents = URLComponents(string: absoluteString) else { return } // Create array of existing query items var queryItems: [URLQueryItem] = urlComponents.queryItems ?? [] // Create query item let queryItem = URLQueryItem(name: name, value: value) // Append the new query item in the existing query items array queryItems.append(queryItem) // Append updated query items array in the url component object urlComponents.queryItems = queryItems // Returns the url from new url components self = urlComponents.url! } } // How to use var url = URL(string: "https://www.example.com")! url.appendQueryItem(name: "name", value: "bhuvan")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With