In the previous article: [iOS] in WebApp How to use JS to call iOS functions, mentioned how to use JS by modifying the URL to invoke iOS intrinsic functions.
There will be a problem, that is, coding problems, such as through the URL to call the pop-up window, write the content: Hello Wanghai.
That's probably the link: http://xxx.com#ios?action=alert¶m= Hello Wanghai
However, when received in iOS, there will be garbled characters in Chinese:
Http://xxx.com#ios?action=alert¶m=%25E6%2596%2587%25E4
The problem with this issue is the encoding of the URL in the conversion, thanks to this blog post: coding issues in iOS.
Encapsulate the transcoding function:
URL encoding-(NSString *) encodetopercentescapestring: (NSString *) input{nsstring *outputstr = (NSString *) CFBridgingRe Lease (Cfurlcreatestringbyaddingpercentescapes (Kcfallocatordefault, (cfstringref ) input, NULL, (cfstringref) @ "!*" ();: @&=+$,/?%#[] ", kCFStringEncodingUTF8)); return OUTPUTSTR;} Decode URL-(NSString *) decodefrompercentescapestring: (NSString *) input{nsmutablestring *outputstr = [NSMutableString Stringwithstring:input]; [Outputstr replaceoccurrencesofstring:@ "+" withstring:@ "" Options:nsliteralsearch range:nsmakerange (0, [outputstr length])]; return [Outputstr stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];}
Demonstrate the encapsulation function above:
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 results are as follows:
>> original:http://search.google.com?keywords= ($# it ' s {a*123}) 00!* ' ();: @&=+$,/?%#[]
>> encoded:http%3a%2f%2fsearch.google.com%3fkeywords%3d%28%24%23%20it%27s%20%7ba%2a123%7d%2900%21%2a%27% 28%29%3b%3a%40%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 your own URL transcoding test code:
nsstring* string1 = @ "https://www.cloudsafe.com/folder"; nsstring* string2 = [string1 stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; nsstring* string3 = [string2 stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; nsstring* String4 = [string2 stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding]; nsstring* string5 = [String3 stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding]; nsstring* String6 = [String4 stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding]; nsstring* String7 = [String5 stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding]; NSLog (@ "raw data%@", string1); NSLog (@ "One layer coding%@", string2); NSLog (@ "Two-layer code%@", String3); NSLog (@ "One layer encoded one layer decoding%@", String4); NSLog (@ "Two-layer encoded one-layer decoding%@", string5); NSLog (@ "One-layer encoded two-layer decoding%@", String6); NSLog (@ "Two-layer encoded two-layer decoding%@", String7);
Printing results:
2014-06-10 15:00:02.425 darewayapp[7400:671651] Raw Data https://www.cloudsafe.com/ folder
2014-06-10 15:00:02.426 darewayapp[7400:671651] one-layer encoding https://www.cloudsafe.com/%E6%96%87%E4%BB%B6%E5%A4%B9
2014-06-10 15:00:02.427 darewayapp[7400:671651] two-layer encoding https://www.cloudsafe.com/%25E6%2596%2587%25E4%25BB%25B6%25E5%25A4%25B9
2014-06-10 15:00:02.427 darewayapp[7400:671651] One -layer decoding of a coded layer https://www.cloudsafe.com/ folder
2014-06-10 15:00:02.427 darewayapp[7400:671651] one-layer decoding of two-layer encoding https://www.cloudsafe.com/%E6%96%87%E4%BB%B6%E5%A4%B9
2014-06-10 15:00:02.427 darewayapp[7400:671651] one-layer coded two-layer decoding https://www.cloudsafe.com/ folder
2014-06-10 15:00:02.427 darewayapp[7400:671651] two-layer coded two-layer decoding https://www.cloudsafe.com/ folder
If the server is using GBK encoding, just change the above UTF to the following encoding:
Nsstringencoding gbkencoding = cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000); URLString = [URLString stringbyreplacingpercentescapesusingencoding:gbkencoding];