Objective-C和Swift URL编码 [英] Objective-C and Swift URL encoding
问题描述
我有一个 NSString
,如下所示:
http://www.
但我想将其转换为:
http%3A%2F%2Fwww.
我该怎么做?
推荐答案
要逃避你想要的角色需要多做一些工作。
To escape the characters you want is a little more work.
示例代码
iOS7及以上:
iOS7 and above:
NSString *unescaped = @"http://www";
NSString *escapedString = [unescaped stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSLog(@"escapedString: %@", escapedString);
NSLog输出:
escapedString:http%3A%2F%2Fwww
escapedString: http%3A%2F%2Fwww
以下是有用的URL编码字符集:
The following are useful URL encoding character sets:
URLFragmentAllowedCharacterSet "#%<>[\]^`{|}
URLHostAllowedCharacterSet "#%/<>?@\^`{|}
URLPasswordAllowedCharacterSet "#%/:<>?@[\]^`{|}
URLPathAllowedCharacterSet "#%;<>?[\]^`{|}
URLQueryAllowedCharacterSet "#%<>[\]^`{|}
URLUserAllowedCharacterSet "#%/:<>?@[\]^`
创建一个结合了以上所有内容的字符集:
Creating a characterset combining all of the above:
NSCharacterSet *URLCombinedCharacterSet = [[NSCharacterSet characterSetWithCharactersInString:@" \"#%/:<>?@[\\]^`{|}"] invertedSet];
创建Base64
在Base64 characterset的情况:
In the case of Base64 characterset:
NSCharacterSet *URLBase64CharacterSet = [[NSCharacterSet characterSetWithCharactersInString:@"/+=\n"] invertedSet];
对于Swift 3.0:
For Swift 3.0:
var escapedString = originalString.addingPercentEncoding(withAllowedCharacters:.urlHostAllowed)
对于Swift 2.x:
For Swift 2.x:
var escapedString = originalString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())
注意: stringByAddingPercentEncodingWithAllowedCharacters
还将编码需要编码的UTF-8字符。
Note: stringByAddingPercentEncodingWithAllowedCharacters
will also encode UTF-8 characters needing encoding.
Pre iOS7使用Core Foundation
使用Core Foundation with ARC:
Pre iOS7 use Core Foundation
Using Core Foundation With ARC:
NSString *escapedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,
(__bridge CFStringRef) unescaped,
NULL,
CFSTR("!*'();:@&=+$,/?%#[]\" "),
kCFStringEncodingUTF8));
使用没有ARC的Core Foundation:
Using Core Foundation Without ARC:
NSString *escapedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)unescaped,
NULL,
CFSTR("!*'();:@&=+$,/?%#[]\" "),
kCFStringEncodingUTF8);
注意: -stringByAddingPercentEscapesUsingEncoding
不会产生正确的编码,在这种情况下,它不会编码返回相同字符串的任何内容。
Note: -stringByAddingPercentEscapesUsingEncoding
will not produce the correct encoding, in this case it will not encode anything returning the same string.
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding
编码14个字符:
`#%^ {} [] | \<>加上空格字符作为百分比转义。
`#%^{}[]|\"<> plus the space character as percent escaped.
testString:
testString:
" `~!@#$%^&*()_+-={}[]|\\:;\"'<,>.?/AZaz"
encodedString:
encodedString:
"%20%60~!@%23$%25%5E&*()_+-=%7B%7D%5B%5D%7C%5C:;%22'%3C,%3E.?/AZaz"
注意:考虑这组字符是否符合您的需求,如果不是根据需要进行更改。
Note: consider if this set of characters meet your needs, if not change them as needed.
需要编码的RFC 3986字符(由于它是编码前缀字符,因此添加了%):
RFC 3986 characters requiring encoding (% added since it is the encoding prefix character):
!#$&'()* +,/:; =?@ []%
"!#$&'()*+,/:;=?@[]%"
一些无保留字符是另外编码:
Some "unreserved characters" are additionally encoded:
\ n \\\\\ n% - 。<> \ ^ _` {|}〜
"\n\r \"%-.<>\^_`{|}~"
这篇关于Objective-C和Swift URL编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!