Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
38 lines (28 sloc) 1.4 KB
//
// NSString+URLEncode.m
//
// Created by Scott James Remnant on 6/1/11.
// Copyright 2011 Scott James Remnant <scott@netsplit.com>. All rights reserved.
//
#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));
}
- (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));
}
- (NSString *)decodeFromURL
{
NSString *decoded = NSMakeCollectable(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)self, CFSTR(""), kCFStringEncodingUTF8));
return [decoded stringByReplacingOccurrencesOfString:@"+" withString:@" "];
}
@end
Something went wrong with that request. Please try again.