Created
February 26, 2013 15:38
-
-
Save gin0606/5039401 to your computer and use it in GitHub Desktop.
AndroidのUri.BuilderみたいにNSURL作れるクラス
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
// | |
// Created by kkgn06 on 2013/02/26. | |
// | |
// To change the template use AppCode | Preferences | File Templates. | |
// | |
#import <Foundation/Foundation.h> | |
@interface KKURLBuilder : NSObject | |
/** | |
* Constructor | |
*/ | |
+ (id)urlBuilder; | |
/** | |
* Sets the scheme. | |
*/ | |
- (KKURLBuilder *)scheme:(NSString *)scheme; | |
/** | |
* sets the authority. | |
*/ | |
- (KKURLBuilder *)authority:(NSString *)authority; | |
/** | |
* Sets the path. | |
*/ | |
- (KKURLBuilder *)path:(NSString *)path; | |
/** | |
* appends to the path. | |
*/ | |
- (KKURLBuilder *)appendPath:(NSString *)newSegment; | |
/** | |
* appends the parameter to the query string. | |
*/ | |
- (KKURLBuilder *)appendQueryParameterKey:(NSString *)key value:(NSString *)value; | |
/** | |
* Constructs a URL with the current attributes. | |
*/ | |
- (NSURL *)buildURL; | |
@end |
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
// | |
// Created by kkgn06 on 2013/02/26. | |
// | |
// To change the template use AppCode | Preferences | File Templates. | |
// | |
#import "KKURLBuilder.h" | |
@interface KKURLBuilder () | |
@property(nonatomic, strong) NSString *scheme; | |
@property(nonatomic, strong) NSString *authority; | |
@property(nonatomic, strong) NSMutableArray *pathArray; | |
@property(nonatomic, strong) NSMutableDictionary *params; | |
@end | |
@implementation KKURLBuilder { | |
} | |
// Constructor | |
+ (id)urlBuilder { | |
return [[self alloc] init]; | |
} | |
- (id)init { | |
if (self = [super init]) { | |
self.params = [NSMutableDictionary dictionary]; | |
self.pathArray = [NSMutableArray array]; | |
} | |
return self; | |
} | |
// Sets the scheme. | |
- (KKURLBuilder *)scheme:(NSString *)scheme { | |
self.scheme = scheme; | |
return self; | |
} | |
// sets the authority. | |
- (KKURLBuilder *)authority:(NSString *)authority { | |
self.authority = authority; | |
return self; | |
} | |
// Sets the path. | |
- (KKURLBuilder *)path:(NSString *)path { | |
self.pathArray = [NSMutableArray arrayWithObject:path]; | |
return self; | |
} | |
// appends to the path. | |
- (KKURLBuilder *)appendPath:(NSString *)newSegment { | |
if ([newSegment hasPrefix:@"/"]) { | |
newSegment = [newSegment substringWithRange:NSMakeRange(1, newSegment.length - 1)]; | |
} | |
if ([newSegment hasSuffix:@"/"]) { | |
newSegment = [newSegment substringWithRange:NSMakeRange(0, newSegment.length - 1)]; | |
} | |
NSString *encodeSegment = [KKURLBuilder encode:newSegment]; | |
[self.pathArray addObject:encodeSegment]; | |
return self; | |
} | |
// appends the parameter to the query string. | |
- (KKURLBuilder *)appendQueryParameterKey:(NSString *)key value:(NSString *)value { | |
[self.params setObject:value forKey:key]; | |
return self; | |
} | |
// Constructs a URL with the current attributes. | |
- (NSURL *)buildURL { | |
NSMutableString *mutableString = [NSMutableString string]; | |
if (self.scheme) { | |
[mutableString appendFormat:@"%@:", self.scheme]; | |
} | |
if (self.authority) { | |
NSString *encodeAuthority = [KKURLBuilder encode:self.authority]; | |
[mutableString appendFormat:@"//%@", encodeAuthority]; | |
} | |
if ([self.pathArray count] > 0) { | |
NSMutableArray *encodePathArray = [NSMutableArray array]; | |
for (NSString *p in self.pathArray) { | |
NSString *encodePath = [KKURLBuilder encode:p]; | |
[encodePathArray addObject:encodePath]; | |
} | |
NSString *path = [encodePathArray componentsJoinedByString:@"/"]; | |
[mutableString appendFormat:@"/%@", path]; | |
} | |
if ([self.params count] > 0) { | |
NSString *query = [self buildQuery]; | |
[mutableString appendFormat:@"?%@", query]; | |
} | |
return [NSURL URLWithString:mutableString]; | |
} | |
// Constructs a query with the current attributes. | |
- (NSString *)buildQuery { | |
NSMutableString *query = [NSMutableString string]; | |
for (NSString *key in self.params) { | |
NSString *value = [self.params objectForKey:key]; | |
[query appendFormat:@"&%@=%@", | |
[KKURLBuilder encode:key], | |
[KKURLBuilder encode:value]]; | |
} | |
// Remove the "&" at the beginning | |
if ([query hasPrefix:@"&"]) { | |
[query deleteCharactersInRange:NSMakeRange(0, 1)]; | |
} | |
return [NSString stringWithString:query]; | |
} | |
+ (NSString *)encode:(NSString *)plainString { | |
NSString *legalURLCharactersToBeEscaped = @"!*'();:@&=+$,/?%#[]"; | |
return (__bridge_transfer NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, | |
(__bridge CFStringRef) plainString, | |
NULL, | |
(__bridge CFStringRef) legalURLCharactersToBeEscaped, | |
kCFStringEncodingUTF8); | |
} | |
+ (NSString *)decode:(NSString *)escapedUrlString { | |
NSString *charsToLeaveEscaped = @""; | |
return (__bridge_transfer NSString *) CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, | |
(__bridge CFStringRef) escapedUrlString, | |
(__bridge CFStringRef) charsToLeaveEscaped, | |
kCFStringEncodingUTF8); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment