Tag Archives: NSQueryItem

Creating and Parsing URL’s with NSURLComponents and NSURLQueryItems

In traditional way, we are creating our API URL’s with formatted strings for some years. We have our format ready, setting parameters and create the URL. This way can be easy but if you do not check the format, you can create wrong URL’s easily. Also, something like this is not good for your eyes;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.yoursite.com/api/%@/register?username=%@&password=%@&email=%@&name=%@&gender=%@&isSubscriber=%@", version, userName, password, email, name, gender, isSubscriber]];

What is 3rd %@? Where is name‘s placeholder?

Creating URL’s from string might be working but is very fragile. If you forgot one & character, URL can be nil and your data will not send to server. In some scenarios, your application could be crashed.

Continue reading