view raw
Zhr Zhr - 9 months ago 82
Objective-C Question

Objective-C - Formatting GET request

What are the rules for passing the string parameter such as %JOHN% in a GET request url

my request url is supposed to look like:

Try#1: I did this

NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"",SEARCH_KEYWORD]];

O/P: nil


NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@""]];


Any suggestions?


You can use NSURLComponents to build URLs:


NSURLComponents *components = [NSURLComponents componentsWithString:@""];
components.query = @"s=%search keywords%"

NSURL *url = components.URL;

Swift (careful with ! in production, I used to test in Playgrounds):

let components = NSURLComponents(string: "")!
components = "s=%search keywords%"

let url = components!
print(url) // ""

Also if you need more complex queries NSURLComponent have a queryItems property.