IOS URLEcode, URLDecode

Source: Internet
Author: User

IOS URLEcode, URLDecode

IOS requires an httpurl interaction with the server when it involves network programming.

If the url contains Chinese characters or the image needs to be uploaded, you must consider urlEncode (otherwise, the url is incorrect)

Some messages returned by the server may also have the same problem. In this case, the server may return urlEcode, and the client needs to decode urlDecode.

 

In fact, the method is very simple. You only need to create the following two methods:

 

Encode URLEncodedString

 

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


 

Decode URLDecodedString

 

-(NSString *)URLDecodedString:(NSString *)str{    NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));        return decodedString;}


 

Call method:

 

 

NSString * readyEcodeStr = @ adsfa asdfa s + asdf; // The NSString * ecodeStr = [self URLEncodedString: readyEcodeStr]; NSLog (@ readyEcodeStr = % @ ecodeStr = % @, readyEcodeStr, ecodeStr );


 

The printed log is as follows:

 

ReadyEcodeStr = adsfa asdfa s + asdf

EcodeStr = adsfa % 20 asdfa % 20 s % 2 Basdf

 

 

OK is basically implemented

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.