NSString+M16String.m 1.31 KB
//
//  NSString+M16String.m
//  Yohoboys
//
//  Created by redding on 14-10-30.
//  Copyright (c) 2014年 YOHO. All rights reserved.
//

#import "NSString+M16String.h"

@implementation NSString (M16String)

#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wdeprecated-declarations"

- (NSString *)encodeToPercentEscapeString
{
    // Encode all the reserved characters, per RFC 3986
    // (<http://www.ietf.org/rfc/rfc3986.txt>)
    NSString *outputStr = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                            (CFStringRef)self,
                                            NULL,
                                            (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                            kCFStringEncodingUTF8));
    return outputStr;
}

- (NSString *)decodeFromPercentEscapeString
{
    NSMutableString *outputStr = [NSMutableString stringWithString:self];
    [outputStr replaceOccurrencesOfString:@"+"
                               withString:@" "
                                  options:NSLiteralSearch
                                    range:NSMakeRange(0, [outputStr length])];
    
    return [outputStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
#pragma clang diagnostic pop
@end