简体   繁体   English

在objective-c中的urldecode

[英]urldecode in objective-c

I'm dealing with an urlencoded string in objective-c. 我正在处理objective-c中的urlencoded字符串。 Is there a foundation function that actually reverse the urlENCODING? 是否存在实际反转urlENCODING的基础功能?

The string received is like: K%FChlschrank but should be after decoding Kühlschrank 收到的字符串如下:K%FChlschrank但应该在解码Kühlschrank之后

I made a quick category to help resolve this :) 我做了一个快速类别来帮助解决这个问题:)

@interface NSString (stringByDecodingURLFormat)
- (NSString *)stringByDecodingURLFormat;
@end

@implementation NSString
- (NSString *)stringByDecodingURLFormat
{
    NSString *result = [(NSString *)self stringByReplacingOccurrencesOfString:@"+" withString:@" "];
    result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return result;
}
@end

Once defined, this quickly can handle an encoded string: 一旦定义,这可以快速处理编码的字符串:

NSString *decodedString = [myString stringByDecodingURLFormat];

Plenty of other ways to implement. 还有很多其他方法可以实现。

I believe this is what you are looking for: 我相信这就是你要找的东西:

- (NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding

Return Value: 返回值:

A new string made by replacing in the receiver all percent escapes with the matching characters as determined by the given encoding. 通过在接收器中替换所有百分比转义而使用由给定编码确定的匹配字符而生成的新字符串。 It returns nil if the transformation is not possible, for example, the percent escapes give a byte sequence not legal in encoding. 如果转换不可能,则返回nil ,例如,百分比转义使得字节序列在编码中不合法。

[source: Apple NSString Class Reference]
- (NSString *)URLDecode:(NSString *)stringToDecode
{
    NSString *result = [stringToDecode stringByReplacingOccurrencesOfString:@"+" withString:@" "];
    result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return result;
}

That's it 而已

Apple has depreacted stringByReplacingPercentEscapesUsingEncoding: since iOS9. Apple已经贬低了stringByReplacingPercentEscapesUsingEncoding:自iOS9以来。 Please use stringByRemovingPercentEncoding . 请使用stringByRemovingPercentEncoding

The new method, Returns a new string made from the receiver by replacing all percent-encoded sequences with the matching UTF-8 characters. 新方法,通过用匹配的UTF-8字符替换所有百分比编码的序列,返回从接收器创建的新字符串。

Example: 例:

NSString *encodedLink = @"hello%20world";
NSString *decodedUrl = [encodedLink stringByRemovingPercentEncoding];
NSLog (@"%@", decodedUrl);

Output: 输出:

hello world

According to W3Schools , URLs can only be sent over the Internet using the ASCII character set. 根据W3Schools ,URL只能使用ASCII字符集通过Internet发送。 For me this piece of code worked: 对我来说,这段代码有效:

NSString *original = @"K%FChlschrank";

NSString *result2 = [original stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

对于iOS 9上的parmanent解决方案,请使用休眠代码

NSURL* link = [NSURL URLWithString:[url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM