url字符串中具有特殊功能的特殊字符的字符串抄肖,或者中文字符,作為參數(shù)用GET方式傳遞時,需要用urlencode處理一下
//URLEncode
? +(NSString*)encodeString:(NSString*)unencodedString{
? ? if([[[UIDevicecurrentDevice]systemVersion]floatValue] >=9.0) {
? ? ? ? ? ? return[unencodedString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
}
NSString*encodedString = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)unencodedString,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8));
returnencodedString;
}//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;
有用戶反饋豁翎,說iOS9之后,CFURLCreateStringByAddingPercentEscapes這個方法廢棄隅忿,所以根據(jù)官方的描述 替換使用stringByAddingPercentEncodingWithAllowedCharacters 這個方法心剥。
stringByAddingPercentEncodingWithAllowedCharacters 需要傳遞參數(shù)NSCharacterSet 。