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