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 的方法
- URLFragmentAllowedCharacterSet "#%<>[]^`{|}
- URLHostAllowedCharacterSet "#%/<>?@^`{|}
- URLPasswordAllowedCharacterSet "#%/:<>?@[]^`{|}
- URLPathAllowedCharacterSet "#%;<>?[]^`{|}
- URLQueryAllowedCharacterSet "#%<>[]^`{|}
- URLUserAllowedCharacterSet "#%/:<>?@[]^`
NSCharacterSet *chaSet = [NSCharacterSet characterSetWithCharactersInString:@"'();:@&=+$,/?%#[]"];
[string stringByAddingPercentEncodingWithAllowedCharacters:chaSet];
网友评论