-
Notifications
You must be signed in to change notification settings - Fork 65
/
NSString+QueryString.m
39 lines (30 loc) · 1.13 KB
/
NSString+QueryString.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// NSString+QueryString.m
// LROAuth2Client
//
// Created by Luke Redpath on 14/05/2010.
// Copyright 2010 LJR Software Limited. All rights reserved.
//
#import "NSString+QueryString.h"
@implementation NSString (QueryString)
- (NSString*)stringByEscapingForURLQuery
{
NSString *result = self;
CFStringRef originalAsCFString = (__bridge CFStringRef) self;
CFStringRef leaveAlone = CFSTR(" ");
CFStringRef toEscape = CFSTR("\n\r?[]()$,!'*;:@&=#%+/");
CFStringRef escapedStr;
escapedStr = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, originalAsCFString, leaveAlone, toEscape, kCFStringEncodingUTF8);
if (escapedStr) {
NSMutableString *mutable = [NSMutableString stringWithString:(__bridge NSString *)escapedStr];
CFRelease(escapedStr);
[mutable replaceOccurrencesOfString:@" " withString:@"+" options:0 range:NSMakeRange(0, [mutable length])];
result = mutable;
}
return result;
}
- (NSString*)stringByUnescapingFromURLQuery
{
return [[self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] stringByReplacingOccurrencesOfString:@"+" withString:@" "];
}
@end