在ios9中replacestringByAddingPercentEscapesUsingEncoding?

在iOS8和之前我可以使用:

NSString *str = ...; // some URL NSString *result = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

在iOS9中, stringByAddingPercentEscapesUsingEncoding已被stringByAddingPercentEncodingWithAllowedCharactersreplace:

 NSString *str = ...; // some URL NSCharacterSet *set = ???; // where to find set for NSUTF8StringEncoding? NSString *result = [str stringByAddingPercentEncodingWithAllowedCharacters:set]; 

我的问题是:在哪里可以find所需的NSCharacterSetNSUTF8StringEncoding )来正确replacestringByAddingPercentEscapesUsingEncoding

弃用信息说(强调我的):

使用stringByAddingPercentEncodingWithAllowedCharacters(_ :)来代替它, 它始终使用推荐的UTF-8编码 ,并且对特定的URL组件或子组件进行编码 ,因为每个URL组件或子组件对于哪些字符有效都有不同的规则。

所以你只需要提供一个足够的NSCharacterSet作为参数。 幸运的是,对于URL,有一个非常方便的类方法叫做URLHostAllowedCharacterSet ,你可以这样使用:

 let encodedHost = unencodedHost.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()) 

更新Swift 3 – 该方法成为静态属性urlHostAllowed

 let encodedHost = unencodedHost.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) 

但请注意:

此方法旨在对URL组件或子组件string进行百分比编码,而不是整个URLstring。

对于Objective-C:

 NSString *str = ...; // some URL NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet]; NSString *result = [str stringByAddingPercentEncodingWithAllowedCharacters:set]; 

哪里可以findNSUTF8StringEncoding?

对于允许百分比编码的六个URL组件和子组件,有预定义的字符集。 这些字符集被传递给-stringByAddingPercentEncodingWithAllowedCharacters:

  // Predefined character sets for the six URL components and subcomponents which allow percent encoding. These character sets are passed to -stringByAddingPercentEncodingWithAllowedCharacters:. @interface NSCharacterSet (NSURLUtilities) + (NSCharacterSet *)URLUserAllowedCharacterSet; + (NSCharacterSet *)URLPasswordAllowedCharacterSet; + (NSCharacterSet *)URLHostAllowedCharacterSet; + (NSCharacterSet *)URLPathAllowedCharacterSet; + (NSCharacterSet *)URLQueryAllowedCharacterSet; + (NSCharacterSet *)URLFragmentAllowedCharacterSet; @end 

弃用信息说(强调我的):

使用stringByAddingPercentEncodingWithAllowedCharacters(_ :)来代替它, 它始终使用推荐的UTF-8编码 ,并且对特定的URL组件或子组件进行编码 ,因为每个URL组件或子组件对于哪些字符有效都有不同的规则。

所以你只需要提供一个足够的NSCharacterSet作为参数。 幸运的是,对于URL,有一个非常方便的类方法叫做URLHostAllowedCharacterSet ,你可以这样使用:

 NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet]; 

但请注意:

此方法旨在对URL组件或子组件string进行百分比编码,而不是整个URLstring。

URLHostAllowedCharacterSet用于我。 我使用URLFragmentAllowedCharacterSet来代替。

目标-C

 NSCharacterSet *set = [NSCharacterSet URLFragmentAllowedCharacterSet]; NSString * encodedString = [@"url string" stringByAddingPercentEncodingWithAllowedCharacters:set]; 

以下是有用的(反转)字符集:

 URLFragmentAllowedCharacterSet "#%<>[\]^`{|} URLHostAllowedCharacterSet "#%/<>?@\^`{|} URLPasswordAllowedCharacterSet "#%/:<>?@[\]^`{|} URLPathAllowedCharacterSet "#%;<>?[\]^`{|} URLQueryAllowedCharacterSet "#%<>[\]^`{|} URLUserAllowedCharacterSet "#%/:<>?@[\]^` 

Objective-C的

这个代码适用于我:

 urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]; 

Swift 2.2:

 extension String { func encodeUTF8() -> String? { //If I can create an NSURL out of the string nothing is wrong with it if let _ = NSURL(string: self) { return self } //Get the last component from the string this will return subSequence let optionalLastComponent = self.characters.split { $0 == "/" }.last if let lastComponent = optionalLastComponent { //Get the string from the sub sequence by mapping the characters to [String] then reduce the array to String let lastComponentAsString = lastComponent.map { String($0) }.reduce("", combine: +) //Get the range of the last component if let rangeOfLastComponent = self.rangeOfString(lastComponentAsString) { //Get the string without its last component let stringWithoutLastComponent = self.substringToIndex(rangeOfLastComponent.startIndex) //Encode the last component if let lastComponentEncoded = lastComponentAsString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.alphanumericCharacterSet()) { //Finally append the original string (without its last component) to the encoded part (encoded last component) let encodedString = stringWithoutLastComponent + lastComponentEncoded //Return the string (original string/encoded string) return encodedString } } } return nil; } } 

对于Swift 3.0

你可以使用urlHostAllowed characterSet。

///返回主机URL子组件允许的字符字符集。

 public static var urlHostAllowed: CharacterSet { get } WebserviceCalls.getParamValueStringForURLFromDictionary(settingsDict as! Dictionary<String, AnyObject>).addingPercentEncoding(withAllowedCharacters: CharacterSet.urlHostAllowed) 

“这个方法是为了对URL组件或子组件string进行百分比编码,而不是整个URLstring”。 ? GeneCode 9月1'16日在8:30

这意味着你不应该编码的URL的https://xpto.example.com/path/subpath ,但只有什么后?

假设,因为在如下情况下有用例:

 https://example.com?redirectme=xxxxx 

其中, xxxxx是完全编码的url。