在ios9中替换stringByAddingPercentEscapesUsingEncoding?

sla*_*vik 108 url encoding objective-c deprecated ios

在iOS8和之前我可以使用:

NSString *str = ...; // some URL
NSString *result = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Run Code Online (Sandbox Code Playgroud)

在iOS9中stringByAddingPercentEscapesUsingEncoding已被替换为stringByAddingPercentEncodingWithAllowedCharacters:

NSString *str = ...; // some URL
NSCharacterSet *set = ???; // where to find set for NSUTF8StringEncoding?
NSString *result = [str stringByAddingPercentEncodingWithAllowedCharacters:set];
Run Code Online (Sandbox Code Playgroud)

我的问题是:在哪里找到所需的NSCharacterSet(NSUTF8StringEncoding)以便正确更换stringByAddingPercentEscapesUsingEncoding

Ant*_*ata 130

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

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

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

let encodedHost = unencodedHost.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())
Run Code Online (Sandbox Code Playgroud)

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

let encodedHost = unencodedHost.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
Run Code Online (Sandbox Code Playgroud)

但请注意:

此方法旨在对URL组件或子组件字符串进行百分比编码,而不是对整个URL字符串进行编码.

  • 另外,我强烈建议使用`NSURLComponents`,它可以为你处理百分比编码. (5认同)
  • 您永远不应该尝试编码整个URL字符串.它可能会导致意外错误,在某些情况下还会导致安全漏洞.对URL进行编码的唯一保证正确方法是一次完成一个. (2认同)

Elo*_*han 98

对于Objective-C:

NSString *str = ...; // some URL
NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet]; 
NSString *result = [str stringByAddingPercentEncodingWithAllowedCharacters:set];
Run Code Online (Sandbox Code Playgroud)

在哪里可以找到NSUTF8StringEncoding的集合?

六个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
Run Code Online (Sandbox Code Playgroud)

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

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

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

NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet]; 
Run Code Online (Sandbox Code Playgroud)

但请注意:

此方法旨在对URL组件或子组件字符串进行百分比编码,而不是对整个URL字符串进行编码.

  • "此方法旨在对URL组件或子组件字符串进行百分比编码,而不是整个URL字符串." ? (5认同)
  • 我喜欢Apple让生活更轻松.谢谢苹果. (4认同)
  • URLHostAllowedCharacterSet给出错误"不支持的URL",我使用了URLFragmentAllowedCharacterSet并且它正常工作. (4认同)

Lal*_*hna 42

URLHostAllowedCharacterSet不工作对我来说.我URLFragmentAllowedCharacterSet改用了.

目标-C

NSCharacterSet *set = [NSCharacterSet URLFragmentAllowedCharacterSet];
NSString * encodedString = [@"url string" stringByAddingPercentEncodingWithAllowedCharacters:set];
Run Code Online (Sandbox Code Playgroud)

SWIFT - 4

"url string".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
Run Code Online (Sandbox Code Playgroud)

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

URLFragmentAllowedCharacterSet  "#%<>[\]^`{|}
URLHostAllowedCharacterSet      "#%/<>?@\^`{|}
URLPasswordAllowedCharacterSet  "#%/:<>?@[\]^`{|}
URLPathAllowedCharacterSet      "#%;<>?[\]^`{|}
URLQueryAllowedCharacterSet     "#%<>[\]^`{|}
URLUserAllowedCharacterSet      "#%/:<>?@[\]^`
Run Code Online (Sandbox Code Playgroud)


小智 21

Objective-C的

这段代码对我有用:

urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
Run Code Online (Sandbox Code Playgroud)