本文介绍了Objective-C 和 Swift URL 编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个这样的 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 codeNSString *unescaped = @"http://www";NSString *escapedString = [unescaped stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];NSLog(@"escapedString: %@", escapedString);NSLog output: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 Base64In the case of Base64 characterset: NSCharacterSet *URLBase64CharacterSet = [[NSCharacterSet characterSetWithCharactersInString:@"/+="] invertedSet];var escapedString = originalString.addingPercentEncoding(withAllowedCharacters:.urlHostAllowed)var escapedString = originalString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())Note: stringByAddingPercentEncodingWithAllowedCharacters will also encode UTF-8 characters needing encoding.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: 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 编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 12:52