Last active
December 15, 2015 20:39
-
-
Save brianmichel/36417ac2c361f560b15f to your computer and use it in GitHub Desktop.
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
#import <Foundation/Foundation.h> | |
/** | |
* A stand-in for `NSURLQueryItem` that can be used on iOS 7. | |
*/ | |
@interface TMURLQueryItem : NSObject | |
/** | |
* @see `NSURLQueryItem` | |
*/ | |
@property (nonatomic, readonly) NSString *name; | |
/** | |
* @see `NSURLQueryItem` | |
*/ | |
@property (nonatomic, readonly) NSString *value; | |
/** | |
* @see `NSURLQueryItem` | |
*/ | |
+ (instancetype)queryItemWithName:(NSString *)name value:(NSString *)value; | |
/** | |
* @see `NSURLQueryItem` | |
*/ | |
- (instancetype)initWithName:(NSString *)name value:(NSString *)value; | |
@end | |
/** | |
* A stand-in for `NSURLComponents` that can be used on iOS 7. Really should only be used if the caller wants to set | |
* `queryItems`. | |
*/ | |
@interface TMURLComponents : NSObject | |
/** | |
* @see `NSURLComponents` | |
*/ | |
@property (nonatomic, readonly) NSURL *URL; | |
/** | |
* @see `NSURLComponents` | |
*/ | |
@property (nonatomic, copy) NSArray/*<TMURLQueryItem>*/ *queryItems; | |
/** | |
* @see `NSURLComponents` | |
*/ | |
- (instancetype)initWithURL:(NSURL *)URL resolvingAgainstBaseURL:(BOOL)resolve; | |
@end |
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
#import "TMURLComponents.h" | |
static inline bool IS_RUNNING_IOS_8_OR_GREATER() { | |
return [[UIDevice currentDevice].systemVersion floatValue] >= 8.0; | |
} | |
@implementation TMURLQueryItem | |
+ (instancetype)queryItemWithName:(NSString *)name value:(NSString *)value { | |
return [[[self class] alloc] initWithName:name value:value]; | |
} | |
- (instancetype)initWithName:(NSString *)name value:(NSString *)value { | |
self = [super init]; | |
if (self) { | |
_name = [name copy]; | |
_value = [value copy]; | |
} | |
return self; | |
} | |
@end | |
@interface TMURLComponents () | |
@property (nonatomic, readonly) NSURLComponents *components; | |
@end | |
@implementation TMURLComponents | |
#pragma mark - Initialization | |
- (instancetype)initWithURL:(NSURL *)URL resolvingAgainstBaseURL:(BOOL)resolve { | |
NSParameterAssert(URL); | |
NSURLComponents *components = [[NSURLComponents alloc] initWithURL:URL resolvingAgainstBaseURL:resolve]; | |
if (!components) { | |
return nil; | |
} | |
self = [super init]; | |
if (self) { | |
_components = components; | |
[self updateQueryItemsWithComponents]; | |
} | |
return self; | |
} | |
- (instancetype)init { | |
return [self initWithURL:nil resolvingAgainstBaseURL:NO]; | |
} | |
#pragma mark - TMURLComponents | |
- (NSURL *)URL { | |
return self.components.URL; | |
} | |
#pragma mark - Properties | |
- (void)setQueryItems:(NSArray *)queryItems { | |
_queryItems = [queryItems copy]; | |
[self updateComponentsWithQueryItems]; | |
} | |
#pragma mark - Private | |
- (void)updateQueryItemsWithComponents { | |
self.queryItems = ^{ | |
NSMutableArray *newQueryItems = [NSMutableArray arrayWithCapacity:self.queryItems.count]; | |
if (IS_RUNNING_IOS_8_OR_GREATER()) { | |
for (NSURLQueryItem *item in self.components.queryItems) { | |
[newQueryItems addObject:[TMURLQueryItem queryItemWithName:item.name value:item.value]]; | |
} | |
} | |
else { | |
NSArray *keyValuePairs = [self.components.query componentsSeparatedByString:@"&"]; | |
for (NSString *keyValuePair in keyValuePairs) { | |
NSArray/*<NSString>*/ *keyAndValue = [keyValuePair componentsSeparatedByString:@"="]; | |
[newQueryItems addObject:[TMURLQueryItem queryItemWithName:keyAndValue.firstObject value:keyAndValue.lastObject]]; | |
} | |
} | |
return newQueryItems; | |
}(); | |
} | |
- (void)updateComponentsWithQueryItems { | |
NSMutableArray *iteratedItemsContainer = [[NSMutableArray alloc] initWithCapacity:[self.queryItems count]]; | |
if (IS_RUNNING_IOS_8_OR_GREATER()) { | |
for (TMURLQueryItem *item in self.queryItems) { | |
[iteratedItemsContainer addObject:[NSURLQueryItem queryItemWithName:item.name value:item.value]]; | |
} | |
self.components.queryItems = iteratedItemsContainer; | |
} | |
else { | |
for (TMURLQueryItem *item in self.queryItems) { | |
[iteratedItemsContainer addObject:[NSString stringWithFormat:@"%@=%@", item.name, item.value]]; | |
} | |
self.components.query = [iteratedItemsContainer componentsJoinedByString:@"&"]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment