ios URL 编码字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21403323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:17:08  来源:igfitidea点击:

URL Encoding a string

iosobjective-cjsonurl

提问by B.I.A

i am receiving a json data object and then i extract a string from it

我正在接收一个 json 数据对象,然后我从中提取一个字符串

NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data
                                                               options:0
                                                                 error:nil];
NSString *country=jsonDictionary[@"address"][@"country"];

then i try to make the string suitable to be used in a URL

然后我尝试使字符串适合在 URL 中使用

NSString *newCountryString = [country stringByReplacingOccurrencesOfString:@" "  
   withString:@"%%20"];

but it is not working

但它不起作用

if i hard coded the newCountryString it would work, why is that ?

如果我对 newCountryString 进行硬编码它会起作用,为什么会这样?

回答by Akshay Nalawade

Use This -

用这个 -

NSString *newCountryString = [country stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

This code will return a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.

此代码将使用给定的编码返回接收器的表示,以确定将接收器转换为合法 URL 字符串所需的转义百分比。

for more details: https://developer.apple.com/documentation/foundation/nsstring/1415058-stringbyaddingpercentescapesusin

更多详情:https: //developer.apple.com/documentation/foundation/nsstring/1415058-stringbyaddingpercentescapesusin

Edit -stringByAddingPercentEscapesUsingEncoding is deprecated in iOS 9. Use following instead

编辑 -stringByAddingPercentEscapesUsingEncoding 在 iOS 9 中已弃用。请改用以下内容

[country stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]

Swift 3 -

斯威夫特 3 -

country.addingPercentEncoding( withAllowedCharacters: .urlHostAllowed)

for more details: https://developer.apple.com/documentation/foundation/nsstring/1411946-stringbyaddingpercentencodingwit?language=objc

更多详情:https: //developer.apple.com/documentation/foundation/nsstring/1411946-stringbyaddingpercentencodingwit?language=objc

回答by Ruslan Soldatenko

As variant you can use method below:

作为变体,您可以使用以下方法:

- (NSString *)URLEncodeStringFromString:(NSString *)string
{
 static CFStringRef charset = CFSTR("!@#$%&*()+'\";:=,/?[] ");
 CFStringRef str = (__bridge CFStringRef)string;
 CFStringEncoding encoding = kCFStringEncodingUTF8;
 return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, str, NULL, charset, encoding));
}

回答by Volker

The NSSString method stringByAddingPercentEscapesUsingEncoding should help you. Also see How to prepare an NSURL from an NSString continaing international characters?

NSSString 方法 stringByAddingPercentEscapesUsingEncoding 应该可以帮助您。另请参阅如何从包含国际字符的 NSString 准备 NSURL?

回答by Sean Dey

I think you should remove one % like below

我认为你应该删除 1%,如下所示

NSString *newCountryString = [country stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

for more

更多

NSMutableString stringByReplacingOccurrencesOfString Warning

NSMutableString stringByReplacingOccurrencesOfString 警告