2012-07-02 64 views

回答

1

當然,你可以選擇這個方法,希望對你有所幫助。

//encode URL string 

+(NSString *)URLEncodedString:(NSString *)str 
{ 

    NSString *encodedString = (NSString *) 
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, 
                   (CFStringRef)str, 
                   NULL, 
                   (CFStringRef)@"!*'();:@&=+$,/?%#[]", 
                   kCFStringEncodingUTF8)); 

    return encodedString; 
} 

} 

//decode URL string 

+(NSString *)URLDecodedString:(NSString *)str 
{ 

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

    return decodedString; 
} 
+0

謝謝! Sameek Mishara – HiroGuo

0

現在不推薦使用stringByAddingPercentEscapesUsingEncoding:。這裏的iOS 7+相當於encodeURIComponent

[str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]