[IOS] Question about URL encoding when using JS to call iOS Functions

Source: Internet
Author: User

In the previous article: [iOS] How to Use JS to call iOS functions in WebApp, I mentioned how to use JS to call iOS Internal functions by modifying the URL.


One problem is the encoding problem. For example, if a pop-up window is called via URL, write the following content in it: Hello, Wang Hai.


The link is like this: http://xxx.com # ios? Action = alert & param = Hello, Wang Hai


However, Chinese garbled characters may appear when the message is received in iOS:

Http://xxx.com # ios? Action = alert & param = % 25E6% 2596% 2587% 25E4

This problem occurs mainly because of the encoding problem in URL conversion. Solution: thanks to this blog post: encoding problem in iOS.


Encapsulate transcoding functions:

// Encode the URL-(NSString *) encodeToPercentEscapeString: (NSString *) input {NSString * outputStr = (NSString *) CFBridgingRelease (partition (kCFAllocatorDefault, (CFStringRef) input, NULL, (CFStringRef )@"! * '();: @ & = + $ ,/? % # [] ", KCFStringEncodingUTF8); return outputStr;} // decodes the URL-(NSString *) encoding: (NSString *) input {NSMutableString * outputStr = [NSMutableString stringWithString: input]; [outputStr replaceOccurrencesOfString: @ "+" withString: @ "" options: NSLiteralSearch range: NSMakeRange (0, [outputStr length])]; return [outputStr failed: NSUTF8StringEncoding];}


Demonstrate the above encapsulated functions:

    NSString * testUrl = @"http://search.google.com?keywords=($# it's {a*123})00!*'();:@&=+$,/?%#[]";    NSLog(@"original: %@", testUrl);    NSString * encodeStr = [self encodeToPercentEscapeString:testUrl];    NSLog(@"encoded: %@", encodeStr);        NSString * encodeStr2 = [testUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    NSLog(@"encoded2:%@", encodeStr2);        NSString * decodeStr = [self decodeFromPercentEscapeString:encodeStr];    NSLog(@"decoded: %@", decodeStr);



The result is as follows:

> Original: http://search.google.com? Keywords = ($ # it's {a * 123}) 00! * '();: @ & = + $ ,/? % # []
> Encoded: http % 3A % 2F % 2Fsearch.google.com % 3 Fkeywords % 3D % 28% 24% 23% 20it % 27 s % 20% 7Ba % 2A123% 7D % 2900% 2A % 21% 27% 28% 3B % 3A % 29% 26% 3D % 2B % 24% 2C % 2F % 3F % 25% 23% 5B % 5D
> Encoded2: http://search.google.com? Keywords = ($ % 23% 20it's % 20% 7Ba * 123% 7D) 00! * '();: @ & = + $ ,/? % 25% 23% 5B % 5D
> Decoded: http://search.google.com? Keywords = ($ # it's {a * 123}) 00! * '();: @ & = + $ ,/? % # []




Demonstrate the built-in URL transcoding test code:

NSString * string1 = @ "string"; NSString * string2 = [string1 encoding: NSUTF8StringEncoding]; NSString * string3 = [string2 encoding: encoding]; NSString * string4 = [string2 encoding: required]; NSString * string5 = [string3 encoding: Required]; NSString * string6 = [string4 encoding: NSUTF8StringEncoding]; NSString * string7 = [string5 encoding: NSUTF8StringEncoding]; NSLog (@ "Raw data % @", string1); NSLog (@ "layer-1 encoding % @", string2); NSLog (@ "two-layer encoding % @", string3 ); NSLog (@ "layer-1 decoding % @", string4); NSLog (@ "layer-2 Decoding % @", string5 ); NSLog (@ "two-layer Decoding for one-layer encoding % @", string6); NSLog (@ "two-layer Decoding for two-layer encoding % @", string7 );



Print result:

15:00:02. 425 DareWayApp [7400: 671651]Raw DataHttps://www.cloudsafe.com/Folder

15:00:02. 426 DareWayApp [7400: 671651]Layer-1 encodingHttps://www.cloudsafe.com/%E6%96%87%E4%BB%B6%E5%A4%B9

15:00:02. 427 DareWayApp [7400: 671651]Two-layer encodingHttps://www.cloudsafe.com/%25E6%2596%2587%25E4%25BB%25B6%25E5%25A4%25B9

15:00:02. 427 DareWayApp [7400: 671651]Layer-1 DecodingHttps://www.cloudsafe.com/Folder

15:00:02. 427 DareWayApp [7400: 671651]Two-layer DecodingHttps://www.cloudsafe.com/%E6%96%87%E4%BB%B6%E5%A4%B9

15:00:02. 427 DareWayApp [7400: 671651]Two-layer DecodingHttps://www.cloudsafe.com/Folder

15:00:02. 427 DareWayApp [7400: 671651]Two-layer DecodingHttps://www.cloudsafe.com/Folder





If the server uses GBK encoding, you only need to change the above UTF to the following encoding:

 NSStringEncoding gbkEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);        urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:gbkEncoding];      



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.