This gist accompanies the blog post Building NSURL Queries with NSURLQueryItems and NSURLComponents.
Last active
March 26, 2020 09:35
-
-
Save joemasilotti/09fe1f247a3da1c782dd to your computer and use it in GitHub Desktop.
Building NSURL Queries with NSURLQueryItems and NSURLComponents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSString *urlString = @"api.twitter.com/1.1/statuses/user_timeline.json?screen_name=joemasilotti"; | |
NSURL *url = [NSURL URLWithString:urlString]; | |
urlString = [urlString stringByAppendingString:@"&include_rts=true"]; | |
url = [NSURL URLWithString:urlString]; | |
NSString *screenName = @"joemasilotti"; | |
NSString *includeRTs = @"true"; | |
urlString = [NSString stringWithFormat:@"api.twitter.com/1.1/statuses/user_timeline.json?screen_name=%@&include_rts=%@", screenName, includeRTs]; | |
url = [NSURL URLWithString:urlString]; | |
NSURLComponents *components = [[NSURLComponents alloc] init]; | |
components.scheme = @"http"; | |
components.host = @"www.api.twitter.com"; | |
components.path = @"/1.1/statuses/user_timeline.json"; | |
components.query = @"screen_name=joemasilotti&include_rts=true"; | |
url = components.URL; | |
NSLog(@"%@", components.queryItems); | |
/* | |
( | |
"<NSURLQueryItem 0x7fbdbb4281b0> {name = screen_name, value = joemasilotti}", | |
"<NSURLQueryItem 0x7fbdbb428250> {name = include_rts, value = true}" | |
) | |
*/ | |
NSURLQueryItem *screenNameItem = [NSURLQueryItem queryItemWithName:@"screen_name" | |
value:@"joemasilotti"]; | |
NSURLQueryItem *includeRTsItem = [NSURLQueryItem queryItemWithName:@"include_rts" | |
value:@"true"]; | |
components.queryItems = @[ screenNameItem, includeRTsItem ]; | |
NSLog(@"%@", components.URL); | |
for (NSURLQueryItem *item in components.queryItems) { | |
if ([item.name isEqualToString:@"screen_name"]) { | |
if ([item.value isEqualToString:@"joemasilotti"]) { | |
return YES; | |
} | |
} | |
return NO; | |
} | |
NSURLQueryItem *item = [NSURLQueryItem queryItemWithName:@"screen_name" | |
value:@"joemasilotti"]; | |
[components.queryItems containsObject:item] should be_truthy; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
While I tried your method, I got following exception on line no 31
-[__NSCFNumber stringByAddingPercentEncodingWithAllowedCharacters:]: unrecognized selector sent to instance 0xb0000000000000a2