Created
December 26, 2016 18:59
-
-
Save skyylex/1ef83a6154f26f12990d2ed35ddc4363 to your computer and use it in GitHub Desktop.
iOS 6+ compatible functions set to update URL parameters
This file contains 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
// | |
// NSURL-Extensions.h | |
// url-modificator | |
// | |
// Created by yury.lapitsky on 26/12/2016. | |
// Copyright © 2016 yury.lapitsky. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#ifndef NSURL_Extensions_h | |
#define NSURL_Extensions_h | |
// Assuming that attribute value separator is `=` and pair delimiter is '&' | |
NSString *buildURLQueryString(NSDictionary *parameters) { | |
NSMutableString *query = [NSMutableString new]; | |
for (NSString *key in parameters.allKeys) { | |
if (query.length > 0) { [query appendString:@"&"]; } | |
NSString *value = parameters[key]; | |
[query appendFormat:@"%@=%@", key, value]; | |
} | |
return query.copy; | |
} | |
// Assuming that attribute value separator is `=` and pair delimiter is '&' | |
NSDictionary *parseURLQueryString(NSString *query) { | |
NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; | |
if (query != nil) { | |
NSArray *parameterPairs = [query componentsSeparatedByString:@"&"]; | |
for (NSString *keyValue in parameterPairs) { | |
if (keyValue == nil || keyValue.length == 0) { continue; } | |
NSArray *components = [keyValue componentsSeparatedByString:@"="]; | |
switch (components.count) { | |
case 0: { | |
// only key, no separator and no value. | |
// valid in general terms of URL. To store key in the dictionary set value as an empty string | |
NSString *key = keyValue; | |
parameters[key] = @""; | |
break; | |
} | |
case 1: { | |
// only key and separator, no value. | |
// valid in general terms of URL. To store key in the dictionary set value as an empty string | |
NSString *key = components.firstObject; | |
parameters[key] = @""; | |
break; | |
} | |
case 2: { | |
// key and value | |
NSString *key = components[0]; | |
NSString *value = components[1]; | |
parameters[key] = value; | |
break; | |
} | |
default: { | |
// invalid parameters pair in the query, skip it | |
} | |
} | |
} | |
} | |
return [NSDictionary dictionaryWithDictionary:parameters]; | |
} | |
// Form: scheme:[//[user:password@]host[:port]][/]path[?query][#fragment] | |
NSURL *replaceQueryInURL(NSURL *url, NSString *query) { | |
if (url.absoluteString.length == 0 || query.length == 0) { return url; } | |
NSMutableString *modifiedURLString = @"".mutableCopy; | |
if (url.scheme != nil) { | |
[modifiedURLString appendFormat:@"%@://", url.scheme]; | |
} | |
if (url.user != nil) { | |
[modifiedURLString appendFormat:@"%@", url.user]; | |
if (url.password != nil) { | |
[modifiedURLString appendFormat:@":%@", url.password]; | |
} | |
} | |
if (url.host != nil) { | |
[modifiedURLString appendFormat:@"%@", url.host]; | |
if (url.port != nil) { | |
[modifiedURLString appendFormat:@"%@", url.port]; | |
} | |
} | |
if (url.relativePath != nil) { | |
modifiedURLString = [modifiedURLString stringByAppendingPathComponent:url.relativePath].mutableCopy; | |
} | |
[modifiedURLString appendFormat:@"?%@", query]; | |
if (url.fragment != nil) { | |
[modifiedURLString appendFormat:@"#%@", url.fragment]; | |
} | |
return [NSURL URLWithString:modifiedURLString]; | |
} | |
NSURL *changeURLQueryParams(NSURL *url, void (^block)(NSMutableDictionary *parameters)) { | |
if (url.absoluteString.length == 0) { return url; } | |
NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; | |
NSString *query = url.query; | |
if (query != nil) { | |
NSDictionary *parsedParameters = parseURLQueryString(query); | |
[parameters addEntriesFromDictionary:parsedParameters]; | |
} | |
block(parameters); | |
NSString *modifiedQuery = buildURLQueryString(parameters); | |
return replaceQueryInURL(url, modifiedQuery); | |
} | |
#endif /* NSURL_Extensions_h */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment