ios中URL字符串的URLencode与URLdecode

2018-05-21  本文已影响22人  二斤寂寞

ios中url经常需要进行encode、decode处理,废话不多,直接高清无码,直逼问题!!!

第一种:比较陈旧

//URLEncode
+ (NSString*) encodeString:(NSString*)unencodedString {
    
    // CharactersToBeEscaped = @":/?&=;+!@#$()~',*";
    // CharactersToLeaveUnescaped = @"[].";
    
    NSString *encodedString = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                              (CFStringRef)unencodedString,
                                                              NULL,
                                                              (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                              kCFStringEncodingUTF8));
    
    return encodedString;
}
//URLDEcode
-(NSString *)decodeString:(NSString*)encodedString

{
    //NSString *decodedString = [encodedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding ];

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

第二种:ios7.0之后

//encode
[string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//decode
[string stringByRemovingPercentEncoding];

第三种:自定义
NSCharacterSet系统内置的一些返回CharacterSet 的方法

NSCharacterSet *chaSet = [NSCharacterSet characterSetWithCharactersInString:@"'();:@&=+$,/?%#[]"]; 
[string stringByAddingPercentEncodingWithAllowedCharacters:chaSet];
上一篇下一篇

猜你喜欢

热点阅读