Fixed iOS 7 support for URL query functions

Summary:
public
Unfortunately, it turns out that NSURLComponents.queryItems only works on iOS 8 and above. This diff re-implements the RCTGetURLQueryParam and RCTURLByReplacingQueryParam functions using functionality available in iOS 7.

Reviewed By: javache

Differential Revision: D2803679

fb-gh-sync-id: 56f10bef4894d16197975b6023b7aa5ab106d8cb
This commit is contained in:
Nick Lockwood 2016-01-05 12:50:25 -08:00 committed by facebook-github-bot-4
parent 2462c8f87d
commit 180fc06b7a
2 changed files with 46 additions and 8 deletions

View File

@ -30,6 +30,13 @@
XCTAssertEqualObjects(bar, @"foo");
}
- (void)testGetEncodedParam
{
NSURL *URL = [NSURL URLWithString:@"http://example.com?foo=You%20%26%20Me"];
NSString *foo = RCTGetURLQueryParam(URL, @"foo");
XCTAssertEqualObjects(foo, @"You & Me");
}
- (void)testQueryParamNotFound
{
NSURL *URL = [NSURL URLWithString:@"http://example.com?foo=bar"];
@ -58,6 +65,13 @@
XCTAssertEqualObjects(result.absoluteString, @"http://example.com?foo=foo&bar=foo");
}
- (void)testReplaceEncodedParam
{
NSURL *URL = [NSURL URLWithString:@"http://example.com?foo=You%20%26%20Me"];
NSURL *result = RCTURLByReplacingQueryParam(URL, @"foo", @"Me & You");
XCTAssertEqualObjects(result.absoluteString, @"http://example.com?foo=Me%20%26%20You");
}
- (void)testAppendParam
{
NSURL *URL = [NSURL URLWithString:@"http://example.com?bar=foo"];

View File

@ -591,9 +591,13 @@ NSString *RCTGetURLQueryParam(NSURL *URL, NSString *param)
}
NSURLComponents *components = [NSURLComponents componentsWithURL:URL
resolvingAgainstBaseURL:YES];
for (NSURLQueryItem *item in components.queryItems.reverseObjectEnumerator) {
if ([item.name isEqualToString:param]) {
return item.value;
// TODO: use NSURLComponents.queryItems once we drop support for iOS 7
for (NSString *item in [components.percentEncodedQuery componentsSeparatedByString:@"&"].reverseObjectEnumerator) {
NSArray *keyValue = [item componentsSeparatedByString:@"="];
NSString *key = [keyValue.firstObject stringByRemovingPercentEncoding];
if ([key isEqualToString:param]) {
return [keyValue.lastObject stringByRemovingPercentEncoding];
}
}
return nil;
@ -607,22 +611,42 @@ NSURL *RCTURLByReplacingQueryParam(NSURL *URL, NSString *param, NSString *value)
}
NSURLComponents *components = [NSURLComponents componentsWithURL:URL
resolvingAgainstBaseURL:YES];
// TODO: use NSURLComponents.queryItems once we drop support for iOS 7
// Unhelpfully, iOS doesn't provide this set as a constant
static NSCharacterSet *URLParamCharacterSet;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSMutableCharacterSet *characterSet = [NSMutableCharacterSet new];
[characterSet formUnionWithCharacterSet:[NSCharacterSet URLQueryAllowedCharacterSet]];
[characterSet removeCharactersInString:@"&=?"];
URLParamCharacterSet = [characterSet copy];
});
NSString *encodedParam =
[param stringByAddingPercentEncodingWithAllowedCharacters:URLParamCharacterSet];
__block NSInteger paramIndex = NSNotFound;
NSMutableArray *queryItems = [components.queryItems mutableCopy];
NSMutableArray *queryItems = [[components.percentEncodedQuery componentsSeparatedByString:@"&"] mutableCopy];
[queryItems enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:
^(NSURLQueryItem *item, NSUInteger i, BOOL *stop) {
if ([item.name isEqualToString:param]) {
^(NSString *item, NSUInteger i, BOOL *stop) {
NSArray *keyValue = [item componentsSeparatedByString:@"="];
if ([keyValue.firstObject isEqualToString:encodedParam]) {
paramIndex = i;
*stop = YES;
}
}];
NSURLQueryItem *newItem = [NSURLQueryItem queryItemWithName:param value:value];
NSString *encodedValue =
[value stringByAddingPercentEncodingWithAllowedCharacters:URLParamCharacterSet];
NSString *newItem = [encodedParam stringByAppendingFormat:@"=%@", encodedValue];
if (paramIndex == NSNotFound) {
[queryItems addObject:newItem];
} else {
[queryItems replaceObjectAtIndex:paramIndex withObject:newItem];
}
components.queryItems = queryItems;
components.percentEncodedQuery = [queryItems componentsJoinedByString:@"&"];
return components.URL;
}