-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNSString+URLEncode.m
41 lines (29 loc) · 1.52 KB
/
NSString+URLEncode.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
40
41
//
// NSString+URLEncode.m
//
// Created by Scott James Remnant on 6/1/11.
// Copyright 2011 Scott James Remnant <[email protected]>. All rights reserved.
//
// encodeForURLFromData: addition by Victor C. Van Hee http://www.totagogo.com
#import "NSString+URLEncode.h"
@implementation NSString (NSString_URLEncode)
- (NSString *)encodeForURL
{
// See http://en.wikipedia.org/wiki/Percent-encoding and RFC3986
// Hyphen, Period, Understore & Tilde are expressly legal
const CFStringRef legalURLCharactersToBeEscaped = CFSTR("!*'();:@&=+$,/?#[]<>\"{}|\\`^% ");
return [NSMakeCollectable(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, legalURLCharactersToBeEscaped, kCFStringEncodingUTF8)) autorelease];
}
- (NSString *)encodeForURLReplacingSpacesWithPlus;
{
// Same as encodeForURL, just without +
const CFStringRef legalURLCharactersToBeEscaped = CFSTR("!*'();:@&=$,/?#[]<>\"{}|\\`^% ");
NSString *replaced = [self stringByReplacingOccurrencesOfString:@" " withString:@"+"];
return [NSMakeCollectable(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)replaced, NULL, legalURLCharactersToBeEscaped, kCFStringEncodingUTF8)) autorelease];
}
- (NSString *)decodeFromURL
{
NSString *decoded = [NSMakeCollectable(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)self, CFSTR(""), kCFStringEncodingUTF8)) autorelease];
return [decoded stringByReplacingOccurrencesOfString:@"+" withString:@" "];
}
@end