共通分類-URLの符号化と復号化

1605 ワード

新規NSStringの分類、URLの符号化と復号化
@interface NSString (URLEncoding)
- (NSString *)URLEncodedString;
- (NSString *)URLDecodedString;
@end
@implementation NSString (URLEncoding)
/*
CFURLCreateStringByAddingPercentEscapes(, , )—Core Foundation  C , URL 

 —  URL , :"! * ()" 

 —  URL 
*/
- (NSString *)URLEncodedString{
    NSString *encodedString = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                              (CFStringRef)unencodedString,
                                                              NULL,
                                                              (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                              kCFStringEncodingUTF8));
    
    return encodedString;
}
- (NSString *)URLDecodedString{
/*
CFURLCreateStringByReplacingPercentEscapes(, , )

 —  
*/
    NSString *decodedString  = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
                                                                                                                     (__bridge CFStringRef)encodedString,
                                                                                                                     CFSTR(""),
                                                                                                                     CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    return decodedString;
}