如何在 iOS 中进行 URL 解码 - Objective C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7920071/
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 22:05:06  来源:igfitidea点击:

How to URL Decode in iOS - Objective C

objective-ciosurldecode

提问by VinnyD

The stringByReplacingPercentEscapesUsingEncoding method is not working properly as it's not decoding special symbols that dont start with a % character, i.e., the + character. Does anyone know of a better method to do this in iOS?

stringByReplacingPercentEscapesUsingEncoding 方法无法正常工作,因为它没有解码不以 % 字符(即 + 字符)开头的特殊符号。有谁知道在 iOS 中执行此操作的更好方法?

Here's what I'm currently using:

这是我目前正在使用的:

NSString *path = [@"path+with+spaces"
     stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

And here's an example of the output:

这是输出的示例:

path+with+spaces

路径+带+空格

回答by rob mayoff

NSString *path = [[@"path+with+spaces"
    stringByReplacingOccurrencesOfString:@"+" withString:@" "]
    stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Note that the plus-for-space substitution is only used in application/x-www-form-urlencodeddata - the query string part of a URL, or the body of a POSTrequest.

请注意,加号替换空格仅用于application/x-www-form-urlencoded数据 - URL 的查询字符串部分或POST请求正文。

回答by delux247

// Decode a percent escape encoded string.
- (NSString*) decodeFromPercentEscapeString:(NSString *) string {
return (__bridge NSString *) CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
                                                        (__bridge CFStringRef) string,
                                                        CFSTR(""),
                                                        kCFStringEncodingUTF8);
} 

http://cybersam.com/ios-dev/proper-url-percent-encoding-in-ios

http://cybersam.com/ios-dev/proper-url-percent-encoding-in-ios

This seems to be the preferred way because... "Apparently" this is a "bug" apple is aware of, but they haven't done anything about it yet... ( http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/)

这似乎是首选方式,因为......“显然”这是一个苹果意识到的“错误”,但他们还没有做任何事情......(http://simonwoodside.com/weblog/2009 /4/22/how_to_really_url_encode/)

回答by Evan Mulawski

If you are trying to replace the plus sign with percent escapes, perform a string replacement from "+" to " " (single space). Then use stringByAddingPercentEscapesUsingEncoding:to add the percent escapes.

如果您尝试用百分比转义替换加号,请执行从“+”到“”(单个空格)的字符串替换。然后用于stringByAddingPercentEscapesUsingEncoding:添加百分比转义。

The plus sign is one of many reserved URL characters that is never encoded.

加号是许多从未编码的保留 URL 字符之一。

(stringByReplacingPercentEscapesUsingEncoding:decodes the percent escapes)

stringByReplacingPercentEscapesUsingEncoding:解码百分比转义)

回答by Mojtabye

swift 2 :

快速2:

extension String  {

    func uriDecodedString() -> String? {
       return self.stringByReplacingOccurrencesOfString("+", withString: " ").stringByRemovingPercentEncoding
    }

}

回答by Fernando Perez

Also you can use the PercentEncoder library from Cocoapods.

你也可以使用 Cocoapods 的 PercentEncoder 库。

Swift 4

斯威夫特 4

  • Include the library to your Podfile:

    pod PercentEncoder

  • Import the library PercentEncoder

    import PercentEncoder

    class ViewController{

    ...

    }

  • Replace the "+" character by "%20" and use the method "ped_decodeURI"

    "path+with+spaces".replacingOccurrences(of: "+", with: "%20").ped_decodeURI()

  • 将库包含到您的 Podfile 中:

    pod 百分比编码器

  • 导入库 PercentEncoder

    导入百分比编码器

    类视图控制器{

    ...

    }

  • 用“%20”替换“+”字符并使用方法“ped_decodeURI”

    "path+with+spaces".replacingOccurrences(of: "+", with: "%20").ped_decodeURI()

It will return "path with spaces"

它将返回“带空格的路径”

Here the link for reference: https://cocoapods.org/pods/PercentEncoder

这里的链接供参考:https: //cocoapods.org/pods/PercentEncoder