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字符串进行编码.
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字符串进行编码.
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)
| 归档时间: |
|
| 查看次数: |
75800 次 |
| 最近记录: |